|
Adding a label between data points |
Posted by Dave on Jun-17-2011 20:46 |
|
I would like to add a text label between data points at the x, y midpoint -- i.e., a label in the middle of the line. (The blue line in the attached screenshot is a line layer in an XYChart. I'm using C++.) Is this possible? If so, could you direct me to the appropriate functions/methods?
|
Re: Adding a label between data points |
Posted by Peter Kwan on Jun-18-2011 00:32 |
|
Hi Dave,
To do what you need, you may:
(a) Add a transparent scatter layer, using the midpoints.
(b) Attach the labels to the scatter points.
For example (in C++)
//compute the midpoints
std::vector<double> midPointY;
std::vector<double> midPointX;
for (int i = 1; i < noOfPoints; ++i) {
midPointX.push_back((myDataX[i - 1] + myDataX[i]) / 2);
midPointY.push_back((myDataY[i - 1] + myDataY[i]) / 2);
}
//add a transparent scatter layer with the midpoints
ScatterLayer *layer = c-addScatterLayer(DoubleArray(&(midPointX[0]), noOfPoints - 1), DoubleArray(&(midPointY[0]), noOfPoints - 1), "", Chart::SquareSymbol, 1, Chart::Transparent, Chart::Transparent);
//associate the labels with the scatter layer
layer->addExtraField(Double(myArrayOfTextStrings, noOfPoints - 1));
layer->setDataLabelFormat("{field0}");
//set the label style
TextBox *t = layer->setDataLabelStyle("arialbd.ttf", 8);
t->setAlignment(Chart::Center);
t->setBackground(0xddddff, 0xddddff);
Hope this can help.
Regards
Peter Kwan |
Re: Adding a label between data points |
Posted by Dave on Jun-18-2011 00:36 |
|
Oh, of course. Thank you, Peter. That's an easy and elegant solution! |
|