|
setDataLabelFormat |
Posted by Joaquin on Aug-30-2013 01:35 |
|
Hi, is there any way to create a data label margin? I have nearby data, and labels are stacked. Attach pic. Thanks.
|
Re: setDataLabelFormat |
Posted by Peter Kwan on Aug-30-2013 23:04 |
|
Hi Joaquin,
There is no general method that can always ensure the data points labels are readable yet not overlapping. For example, if you have 20 points crowded togther within a few pixels, it is hard to label the points without overlapping. If you distribute the labels so that they do not overlap, then it is hard to associate the labels with the points.
However, for specific cases, there are often methods available. ChartDirector allows you to control the position of each label, so depending on the nature of your data, it may be possible to position the labels so that they do not ovelap.
For the specific case of having two data series using the same x-coordinates, for each x-position, you can always put one label above the higher point, and another label below the lower point. In this way, they cannot overlap.
For your case with 3 data series using the same x-coordinates, in which the horizontal spacing of the data points are wide enough, you may use the following method:
For each position, for the lowest point, put the label below it. For the highest point, put the label at the top-left corner of the point. For the middle point, put the label at the top-right corner of the point. In C#/Java, it is like:
// For each x-coordinate
for (int i = 0; i < data0.Length; ++i) {
// Determine the top, middle and bottom points
double[] points = { data0[i], data1[i], data2[i] };
int maxIndex = 0, minIndex = points.Length - 1;
for (int j = 0; j < points.Length; ++j) {
if (points[j] > points[maxIndex]) maxIndex = j;
if (points[j] < points[minIndex]) minIndex = j;
}
int midIndex = 3 - maxIndex - minIndex;
// Add labels with different alignments with the data points
layer.addCustomDataLabel(maxIndex, i, "{value}", "arialbd.ttf", 8).setAlignment(Chart.BottomRight);
layer.addCustomDataLabel(midIndex, i, "{value}", "arialbd.ttf", 8).setAlignment(Chart.BottomLeft);
layer.addCustomDataLabel(minIndex, i, "{value}", "arialbd.ttf", 8).setAlignment(Chart.Top);
}
Hope this can help.
Regards
Peter Kwan |
Re: setDataLabelFormat |
Posted by Sherri on Mar-10-2016 17:17 |
|
Hi Peter,
Is there a way to write the C#/Java script in your reply to Perl? |
Re: setDataLabelFormat |
Posted by Peter Kwan on Mar-11-2016 01:44 |
|
Hi Sherri,
I have translated the code as below. (I have not tested it myself, so there may be typo mistakes.)
# Assume $data0, $data1 and $data2 are array references that contains the data
for (my $i = 0; $i < scalar(@$data0); ++$i) {
# Determine the top, middle and bottom points
my $points = [ $data0->[$i], $data1->[$i], $data2->[$i] ];
my $maxIndex = 0;
my $minIndex = scalar(@$points) - 1;
for (my $j = 0; $j < scalar(@$points); ++$j) {
if ($points->[$j] > $points->[$maxIndex]) { $maxIndex = $j; }
if ($points->[$j] < $points->[$minIndex]) { $minIndex = $j; }
}
my $midIndex = 3 - $maxIndex - $minIndex;
# Add labels with different alignments with the data points
$layer->addCustomDataLabel($maxIndex, $i, "{value}", "arialbd.ttf", 8)->setAlignment($perlchartdir::BottomRight);
$layer->addCustomDataLabel($midIndex, $i, "{value}", "arialbd.ttf", 8)->setAlignment($perlchartdir::BottomLeft);
$layer->addCustomDataLabel($minIndex, $i, "{value}", "arialbd.ttf", 8)->setAlignment($perlchartdir::Top);
}
Regards
Peter Kwan |
|