score:1

Accepted answer

After much deleting parts of my example SVG image, to find when the error would go away, I found that it's the tspan tags - once I leave them out, canvg will display text only once as intended.

score:0

<!-- Client Side Code -->
<script type="text/javascript">
    window.onload = function() {
        //1. Get the Highcharts Graph from the Div containing it
        var chart = $('#IDOfDivContainingHighchartsGraph').highcharts();

        //2. Get SVG from Hicharts Export Service
        var svgString = chart.getSVGForExport();

        //3. Save it client side div to manipulate the fetched string
        $('#exportedSVG').html(svgString);

        //4. LOGIC TO REMOVE THE DUPLICATE TSPAN TAG
        var svgTSPAN_TextElement=$("#exportedSVG text");
        for(i=0;i<svgTSPAN_TextElement.length;i++){
            childTspanElements=$(svgTSPAN_TextElement[i]).children("tspan");
            if(childTspanElements.length==2){
                childTspanElements[1].remove();
            }
        }

        //5. Get the maniupluated object as string
        svgString=$("#exportedSVG svg").get(0).outerHTML;

        //6. POST Maniiulated SVG string
        var url= '/URL/To/Server/Side.php';
        $.ajax({
            type: 'POST',
            data: {svgString:svgString, upload:1},
            url: url,
            async: false,
            success: function(data){
                //Nothing here
            }
        });

    }
</script>

//Server Side Code
<?php
if(isset($_POST["upload"])){
    $SVGData=$_POST["svgString"];
    $locationToSaveFile = "/location/to/Save/SVGFILE.svg";
    file_put_contents($locationToSaveFile, $SVGData);
    exit();
}

//By the end of this code you would have created the svg file of the hicharts on the server side
?>

score:1

I just added textOutline: 'none' inside your exporting parameter and it works for me.

Codpen with solution https://codepen.io/kirill-kirs/pen/qBjNdwg

 exporting: {
        sourceWidth: 1000,
        sourceHeight: 1000,
        chartOptions: {
            plotOptions: {
                series: {
                    dataLabels: {
                        enabled: true,
                        backgroundColor: 'rgba(0, 0, 0, 0.75)',
                        style: {
                            textOutline: 'none',
                            color: 'white'
                        }
                    }
                }
            }
        }
    }

score:4

This question is a bit old but I also found this pretty annoying to have this double text bug in my graphs. I took a look at the code and modified it to work with tspan. I didn't really dig into it, so I don't understand all the mechanisms taking place in the library, but I noticed that for "tspan", at some point, the object created was :

  • Well, an object, with a type "tspan" and a text attribute.
  • And that object also contained an other object, which is the text value of the tspan (which is then the same as the previous text attribute)

So what I did is to modify the source code of the library. If you search for

// tspan 
svg.Element.tspan = function(node) {

Then you just have to add this inside the function (replacing the old content) :

if ( node.nodeName == "tspan")
    this.text = '' ;
else
    this.text = node.value || node.text || node.textContent || '';

And that solved my problem. I hope that helps someone.


Related Query

More Query from same tag