ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  setTruncate not working on legend
Posted by at on Apr-22-2020 17:57
Hi, can you tell me any possible reasons why the setTruncate API is not working? It still displays the whole set of legend. Below is the snippet of the code where I'm trying to insert it.

...
    $legend_obj = $this->c_obj->addLegend2(1, 1, $this->getLegendNoOfCols(), $this->getLegendFont(), $this->getLegendFontSize());
    $legend_obj->setFontColor($this->getLegendFontColor());
    $legend_obj->setBackground($this->getLegendBackgroundColor(), $this->getLegendEdgeColor());
    $legend_obj->setKeyBorder($this->getLegendKeyBorder());
    $legend_obj->setMargin($this->getLegendMargin());
    // here i try to call it to limit the legend to 2 lines for example, but not working
   $legend_obj>setTruncate($this->getPlotAreaWidth(), 2);
...


Thanks.

  Re: setTruncate not working on legend
Posted by Peter Kwan on Apr-23-2020 04:07
Attachments:
Hi at,

The legend box contains many legend items. The setTruncate refers to the number of lines in a legend item. For example, if you have a data set with a long name, like:

$c->addLineLayer($data, $color, "This is a very long data set name abc def ghi 111 222 333");

then it is possible the name in the legend item will be in multiple lines. The setTruncate can be used to limit the number of lines for that name.

If your legend box is using horizontal flow layout, the only method to control the number of items so as not to exceed two lines is to draw the legend box with your own code. For PHP, the "Track Line with Legend" is an example in which the legend box is drawn using Javascript on the browser side. I have also attached an example in which the legend is drawn on the server side using CDML.

Regards
Peter Kwan
multiline2.php
<?php
require_once("../lib/phpchartdir.php");

#
# Draw the track line with legend
#
function drawLegend(&$c)
{
    # Use Line Style icons
    $c->getLegend()->setLineStyleKey();

    # Container to hold the legend entries
    $legendEntries = array();

    # Iterate through all layers to build the legend array
    for($i = 0; $i < $c->getLayerCount(); ++$i) 
    {
        $layer = $c->getLayerByZ($i);

        # Iterate through all the data sets in the layer
        for($j = 0; $j < $layer->getDataSetCount(); ++$j) 
        {
            $dataSet = $layer->getDataSetByZ($j);

            # We are only interested in visible data sets with names
            $dataName = $dataSet->getDataName();
            $color = $dataSet->getDataColor();
            if ($dataName && ($color != Transparent))
                # Build the legend entry, consist of the legend icon and name.
                $legendEntries[] = "<*block*>" . $dataSet->getLegendIcon() . " " . $dataName . "<*/*>";
        }
    }

    # The plot area object
    $plotArea = $c->getPlotArea();
    
    # Create the legend by joining the legend entries
    $legendText = "<*block,maxWidth=" . $plotArea->getWidth() . ",truncate=2*><*block*>" . join("        ", array_reverse($legendEntries)) . "<*/*>";

    # Display the legend on the top of the plot area
    $c->addText($plotArea->getLeftX() + 5, $plotArea->getTopY() - 3, $legendText, "arial.ttf", 12, 0x000000, BottomLeft);
}


# In this example, we simply use random data for the 3 data series.
$r = new RanSeries(129);
$data0 = $r->getSeries(100, 100, -15, 15);
$data1 = $r->getSeries(100, 160, -15, 15);
$data2 = $r->getSeries(100, 220, -15, 15);
$timeStamps = $r->getDateSeries(100, chartTime(2014, 1, 1), 86400);

# Create a XYChart object of size 600 x 400 pixels
$c = new XYChart(600, 400);

# Add a title box using grey (0x555555) 20pt Arial font
$c->addTitle("    Multi-Line Chart Demonstration", "arial.ttf", 20, 0x555555);

# Set the plotarea at (70, 70) and of size 500 x 300 pixels, with transparent background and border
# and light grey (0xcccccc) horizontal grid lines
$c->setPlotArea(70, 70, 500, 300, Transparent, -1, Transparent, 0xcccccc);

# Set axis label font to 12pt Arial
$c->xAxis->setLabelStyle("arial.ttf", 12);
$c->yAxis->setLabelStyle("arial.ttf", 12);

# Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa)
$c->xAxis->setColors(Transparent, TextColor, TextColor, 0xaaaaaa);
$c->yAxis->setColors(Transparent);

# Set the major/minor tick lengths for the x-axis to 10 and 0.
$c->xAxis->setTickLength(10, 0);

# For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis.
$c->xAxis->setTickDensity(80);
$c->yAxis->setTickDensity(40);

# Add a title to the y axis using dark grey (0x555555) 14pt Arial font
$c->yAxis->setTitle("Y-Axis Title Placeholder", "arial.ttf", 14, 0x555555);

# Add a line layer to the chart with 3-pixel line width
$layer = $c->addLineLayer2();
$layer->setLineWidth(3);

# Add 3 data series to the line layer
$layer->addDataSet($data0, 0x5588cc, "Alpha");
$layer->addDataSet($data1, 0xee9944, "Beta");
$layer->addDataSet($data2, 0x99bb55, "Gamma");

# The x-coordinates for the line layer
$layer->setXData($timeStamps);

drawLegend($c);

# Output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
?>

  Re: setTruncate not working on legend
Posted by at on Apr-23-2020 15:21
Hi Peter, tried and it's working but wondering why the minData/maxData legends of the boxwhisker is not appearing, only those of the boxTop/boxBottom?. The following snippet is how we constructed it, we put each element into separated layers.

...
$mid_obj = $this->c_obj->addBoxWhiskerLayer(
                null,
                null,
                null,
                null,
                $data['median'],
                $data['median_fill_color'],
                $data['median_line_color'],
                $data['median_line_color']
            );
            $mid_obj->addDataSet(null, $data['median_color'], $data['median_legend']);
            $mid_obj->setLineWidth($data['median_line_width']);

            $q1q3_obj = $this->c_obj->addBoxWhiskerLayer(
                $data['percentile_75'],
                $data['percentile_25'],
                null,
                null,
                null,
                $q1q3_fill_color,
                $data['q1q3_line_color'],
                $data['q1q3_line_color']
            );
            $q1q3_obj->addDataSet(null, $q1q3_fill_color, $data['q1q3_legend']);
            $q1q3_obj->setLineWidth($data['q1q3_line_width']);

            $minmax_obj = $this->c_obj->addBoxWhiskerLayer(
                null,
                null,
                $data['max_result'],
                $data['min_result'],
                null,
                $data['minmax_fill_color'],
                $data['minmax_line_color'],
                $data['minmax_line_color']
            );
            $minmax_obj->addDataSet(null, $data['minmax_line_color'], $data['minmax_legend']);
            $minmax_obj->setLineWidth($data['minmax_line_width']);
            $minmax_obj->setDataGap(0.5);
...

Thanks.

  Re: setTruncate not working on legend
Posted by at on Apr-23-2020 16:00
Hi Peter, please disregard my previous reply, it's actually the added legendKeys that are not shown. Is there a way to merge those with your solution?

Thanks.

  Re: setTruncate not working on legend
Posted by Peter Kwan on Apr-24-2020 01:45
Hi at,

I am not too sure what is not shown in your case.

The drawLegend function in my previous code is just an example of using CDML. It draws a legend icon for any dataset that has a color and a name. For your code, there are 3 names with 3 colors, so I would expect the legend will contain 3 items for:

(1) $data['median_color'], $data['median_legend']
(2) $q1q3_fill_color, $data['q1q3_legend']
(3) $data['minmax_line_color'], $data['minmax_legend']

If you want to add other entries, you can just add more datasets with the color and text you want to display. The dataset can be empty so it woud not affect your chart.

Regards
Peter Kwan

  Re: setTruncate not working on legend
Posted by at on Apr-24-2020 08:53
Hi Peter, you might have missed my followup message to disregard my previous reply. The legends that were not shown were actually the ones added using the $legendBox->addKey() method. Is there a way to extract those and add them to the legendEntries of the drawLegend() of your attached multiline2.php?

Thanks.

  Re: setTruncate not working on legend
Posted by Peter Kwan on Apr-24-2020 10:21
Hi at,

Instead of adding the keys in the LegendBox, you can add the keys by adding a new empty dataset or layer. For example, instead of:

$legendBox->addKey("abcd", 0x123456);

you can use:

$c->addLineLayer(array(), 0x123456, "abcd");

Or you can create a dummy layer and add the keys as empty datasets, like:

$legendKeyLayer = $c->addLineLayer2();

$legendKeyLayer->addDataSet(array(), 0x123456, "abcd");
$legendKeyLayer->addDataSet(array(), 0x999999, "another key");

Hope this can help.

Regards
Peter Kwan

  Re: setTruncate not working on legend
Posted by at on Apr-24-2020 13:11
Thanks Peter, I was just hoping there is a way of retrieving those keys.