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

Message ListMessage List     Post MessagePost Message

  perl: word wrap dataset name for XYChart
Posted by Donavon Lerman on Mar-20-2019 05:27
Attachments:
How do I get the dataset names to word warp?

Example of non-word wrapping chart attached.

+--------------------------------------------+

#!/usr/bin/perl
use File::Basename;
use lib dirname($0) =~ /(.*)/;

use perlchartdir;

my $data0                    = [200];
my $data1                    = [200];
my $data2                    = [200];
my $data3                    = [200];
my $data4                    = [200];
my $data5                    = [200];
my $data6                    = [200];

my $label0                   = 'word wrap word wrap word wrap';
my $label1                   = 'word wrap word wrap word wrap';
my $label2                   = 'word wrap word wrap word wrap';
my $label3                   = 'word wrap word wrap word wrap';
my $label4                   = 'word wrap';
my $label5                   = 'word wrap';
my $label6                   = 'word wrap';

my $imgDir                  = "/tmp/tmpcharts";

my $c = new XYChart(1000, 75);
$c->setPlotArea(5, -5, $c->getWidth() - 10, $c->getHeight() +10, -1, -1, $perlchartdir::Transparent, $perlchartdir::Transparent, $perlchartdir::Transparent);
$c->swapXY();

# Set axes to transparent
$c->xAxis()->setColors($perlchartdir::Transparent, $perlchartdir::Transparent);
$c->yAxis()->setColors($perlchartdir::Transparent, $perlchartdir::Transparent);
$c->yAxis()->setAutoScale(0, 0, 1);
$c->yAxis()->setRounding(0, 0);

my $layer = $c->addBarLayer2($perlchartdir::Stack);
$layer->setMinLabelSize(5);

$layer->setDataLabelStyle("arialbd.ttf", 12, 'black')->setAlignment($perlchartdir::Center);

$layer->setDataLabelFormat("{dataSetName}");

$layer->addDataSet($data0, -1, $label0);
$layer->addDataSet($data1, -1, $label1);
$layer->addDataSet($data2, -1, $label2);
$layer->addDataSet($data3, -1, $label3);
$layer->addDataSet($data4, -1, $label4);
$layer->addDataSet($data5, -1, $label5);
$layer->addDataSet($data6, -1, $label6);

# Create the image and save it in a temporary location
my $chartURL = $c->makeTmpFile($imgDir);

# Create an image map for the chart
my $imageMap = $c->getHTMLImageMap("", "",    "title='{dataSetName} : {value}'");

print "Content-type: text/htmlnn";
print <<EndOfHTML
<html>
<body style="margin: 0; padding: 0;">
    <div style="    padding: 70px 0;
    border: 0px;
    text-align: center;">
        <img src="getchart.pl?img=$imgDir/$chartURL" border="0" usemap="#map1">
        <map name="map1">
        $imageMap
        </map>
    </div>
</body>
</html>
EndOfHTML
cd__192.168.10.11359123123241553030582_0.png

  Re: perl: word wrap dataset name for XYChart
Posted by Peter Kwan on Mar-20-2019 19:07
Hi Donavon,

You can insert a linefeed into the data set name to wrap them, like "abc\ndef";

For data labels, there is a way to automatically wrap a label when it exceeds a certain length by using TextBox.setMaxWidth. It is like:

$t = $layer->setDataLabelStyle("arialbd.ttf", 12, 0x000000);
$t->setAlignment($perlchartdir::Center);
$t->setMaxWidth(200);   // wrap when over 200 pixels long

However, the above works only if your can determine the maximum width. For example, if it is a vertical bar chart, the bar width for every bar can easily be determined to be  (plot_area_width / bar_count) * 0.75 (assume the default bar gap of 0.25).

For your case, if every bar segment must be of the same width, you can use the method above as well. If the bar width depends on data and can be arbitrary (which means the bar segment can be very long or can be as short as 1 pixel), you would need to compute the bar segment width one by one with your own code, then use Layer.addCustomDataLabel or BaseChart.addText to add the labels, which allows you to set a different maximum width for each label. If you need further details of this method, please let me know.

Note that in the latter case, as the bar can be very short, there is no guarantee that any text can be displayed all. So an alternative way to display the labels may be necessary or preferable, such as using a legend box.

Regards
Peter Kwan

  Re: perl: word wrap dataset name for XYChart
Posted by Donavon Lerman on Mar-21-2019 00:50
How do I make the $t->setMaxWidth() dynamic based on the size of each bar segment.

my $data0                    = [20];
my $data1                    = [100];
my $data2                    = [55];
my $data3                    = [63];
my $data4                    = [10];
my $data5                    = [34];
my $data6                    = [20];

my $label0                   = 'word wrap word wrap word wrap';
my $label1                   = 'word wrap word wrap word wrap';
my $label2                   = 'word wrap word wrap word wrap';
my $label3                   = 'word wrap word wrap word wrap';
my $label4                   = 'word wrap';
my $label5                   = 'word wrap';
my $label6                   = 'word wrap';

  Re: perl: word wrap dataset name for XYChart
Posted by Peter Kwan on Mar-21-2019 18:04
Attachments:
Hi Donavon,

An example is as follows. Note the part of the code after $c->layoutAxes();. It adds a custom label for each bar segment, computes the bar width, and use it as the maximum width.

my $allData = [$data0, $data1, $data2, $data3, $data4, $data5, $data6];
my $allLabels = [$label0, $label1, $label2, $label3, $label4, $label5, $label6];

my $c = new XYChart(1000, 75);
$c->setPlotArea(5, -5, $c->getWidth() - 10, $c->getHeight() +10, -1, -1, $perlchartdir::Transparent, $perlchartdir::Transparent, $perlchartdir::Transparent);
$c->swapXY();

# Set axes to transparent
$c->xAxis()->setColors($perlchartdir::Transparent, $perlchartdir::Transparent);
$c->yAxis()->setColors($perlchartdir::Transparent, $perlchartdir::Transparent);
$c->yAxis()->setAutoScale(0, 0, 1);
$c->yAxis()->setRounding(0, 0);

my $layer = $c->addBarLayer2($perlchartdir::Stack);
for(my $i = 0; $i < scalar(@$allData); ++$i) {
    $layer->addDataSet($allData->[$i], -1, $allLabels->[$i]);
}

$c->layoutAxes();

for(my $j = 0; $j < scalar(@$data0); ++$j) {
    my $acc = 0;
    for(my $i = 0; $i < scalar(@$allData); ++$i) {
        my $textbox = $layer->addCustomDataLabel($i, $j, $allLabels->[$i], "arialbd.ttf", 12, 0x000000);
        $textbox->setAlignment($perlchartdir::Center);
        my $dataValue = $allData->[$i]->[$j];
        $textbox->setMaxWidth(abs($layer->getYCoor($acc + $dataValue) - $layer->getYCoor($acc)));
        $acc += $dataValue;
    }
}

# Create the image and save it in a temporary location
my $chartURL = $c->makeTmpFile($imgDir);


Regards
Peter Kwan
hbar.png