|
Problem working with BarLayers and LineLayers on the same chart |
Posted by Davide on Apr-23-2020 17:39 |
|
Hi,
I'm struggling to find a solution to my problem. I'm working with C++ library of chartdir and what I need to do is to use both BarLayers and LineLayers on the same chart. I've always used them separately and now I really can't get them to work toghether.
this is a piece of code that explains how I'm implementing my solution:
...
DoubleArray viewPortDataSeriesA = DoubleArray(arrayCumulativo.data(), arrayCumulativo.size());
DoubleArray viewPortDataSeriesB = DoubleArray(arrayRelativo->data(), arrayRelativo->size());
LineLayer *layer = c->addLineLayer();
layer->setFastLineMode();
layer->setXData(viewPortTimeStamps);
layer->addDataSet(ArrayMath(viewPortDataSeriesA).movAvg(0).trim(0).result(), pStrutturaComp->at(h)->colore.at(j), "1");
BarLayer *bar = c->addBarLayer(Chart::Side);
bar->setBarWidth(100, 100/arrayRelativoTot.size());
for(int i = 0; i < arrayRelativoTot.size(); i++)
{
if(indexColor == indexPosOfRef)
indexColor++;
QVector<double> tragBar;
for(int p = 0; p < tragnum.size(); p++)
{
tragBar.append(static_cast<double>(p) + 0.5);
}
viewPortTimeStamps = DoubleArray(tragBar.data(), tragBar.size());
viewPortDataSeriesB = DoubleArray(arrayRelativoTot.at(i)->data(), arrayRelativoTot.at(i)->size());
bar->addDataSet(ArrayMath(viewPortDataSeriesB).movAvg(0).trim(0).result(), colore.at(indexColor), "1");
bar->setXData(viewPortTimeStamps);
// bar->setBarGap(0.2, Chart::TouchBar);
bar->setBorderColor(Chart::Transparent, Chart::glassEffect(Chart::NormalGlare, Chart::Left));
}
for(int i = 0; i < tragnum.size(); i++)
{
c->xAxis2()->addMark(tragnum.at(i), 0xDDDDDD, ("i" + QString::number(i + 1)).toLatin1()/*,"arialbd.ttf", 10*/);
}
c->yAxis()->setColors(M_BIANCO,M_BIANCO,M_BIANCO,M_BIANCO);
c->yAxis()->setLabelStyle(LABEL_STYLE,LABEL_FONT, M_BIANCO)->setFontAngle(0,false);
c->yAxis()->setTitle("DELTA SECTORS",TITLE_STYLE,FONT_TITLE,M_BIANCO)->setFontAngle(0,false);
c->yAxis()->setTitlePos(Y_TITLE_AL_2, Y_TITLE_GAP_2);
double delta = (max - min)*PERCENTAGE_SCALE;
c->yAxis()->setRounding(false, false);
c->yAxis()->setLinearScale(min - delta, max + delta);
c->yAxis()->setTickDensity(TICK_DENSITY_YAXIS);
c->xAxis()->setTickDensity(TICK_DENSITY_XAXIS);
c->xAxis()->setDateScale();
...
That's just the piece of code where I draw the layers. The fact is that I would like to have instead of a DoubleArray as the XData I just need numbers forom 1 to 10 for both BarLayers and LineLayers.
But I want the BarLayers to be in the middle of two sectors like I've done on the image.
Another question is, How do I delete the spare that there is from the beginning of the chart and the point 0 and also on the right side? (I used two arrows to indicate them)
I'm sorry for my bad english, I hope that from the image you can understand better what I mean.
Thank you in advance.
|
Re: Problem working with BarLayers and LineLayers on the same chart |
Posted by Peter Kwan on Apr-23-2020 23:49 |
|
Hi Davide,
In your code, you use "viewPortDataSeriesA". In our sample code, the viewPortXXX variables are usually used in zoomable and scrollable charts. In this type of charts, the user can zoom in/out, so the x-axis scale is controlled by the user. For example, the user can zoom to the range x = 3.8914 to 8.9485 by dragging a selection rectangle.
However, in your code, it seems it is not using zooming features, so I assume your code directly controls the x-axis scale.
In your case, it is seems the x-coordinates for the bars are from 0.5 to tragnum.size() - 0.5. It is not clear what is the x-coordinates used for your line. My guess is that it is from 0 to tragnum.size() - 1. The x-axis range must include both layers, so it needs to be 0 to tragnum.size() - 0.5 (or 0 to 9.5 assuming traqnum.size() == 10).
For a bar chart, ChartDirector will automatically add 0.5 units of empty space to either side of the x-axis. It is because the bar has a width. The bar at x = 0 means the center of the bar is at x = 0. So the bar must occupy some space to the left of x = 0, and we need to reserve some space for that. You can remove the 0.5 units of empty space by:
c->xAxis()->setIndent(false);
You mentioned you want the bars to be in between the line data points. So if there are 10 data points for the line, there must be 9 positions for the bars. So instead of:
for(int p = 0; p < tragnum.size(); p++)
{
tragBar.append(static_cast<double>(p) + 0.5);
}
you may try:
for(int p = 0; p < tragnum.size() - 1; p++)
{
tragBar.append(static_cast<double>(p) + 0.5);
}
This will shorten the bar x-range to 0.5 to 8.5, which should be what you want.
Hope this can help.
Regards
Peter Kwan |
Re: Problem working with BarLayers and LineLayers on the same chart |
Posted by Davide on Apr-24-2020 16:14 |
|
Thanks a lot Peter, it was exactly what I was searching for.
I resolved it using c->xAxis()->setLinearScale(0, tragnum.size() - 1, 1, 0) and using c->xAxis()->setIndent(false), giving the bar chart a different XData with 0.5 offset so that it cis displayed in between the sectors!
Thanks again! |
|