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

Message ListMessage List     Post MessagePost Message

  Problem of LinearScale with Bar chart
Posted by Herv? Thomy on Nov-10-2008 18:28
Attachments:
hi,

I have a problem with my Bar chart.

I want to scale my x Axis, but my bar Chart is not represented on the whole draw area. What's the matter ?

Here is the code I've written :

#include "chartdir.h"

int main(int argc, char *argv[])
{
double xData[] = { -0.5, -0.4, -0.3, -0.2, -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5 };
double yData[] = { 1, 3, 6, 10, 5, 0, 0, 12, 20, 4, 1};

    // Create a XYChart object of size 600 x 300 pixels
    XYChart *c = new XYChart(600, 300);

    // Set the plotarea at (50, 35) and of size 500 x 240 pixels. Enable both the
    // horizontal and vertical grids by setting their colors to grey (0xc0c0c0)
    c->setPlotArea(50, 35, 500, 240)->setGridColor(0xc0c0c0, 0xc0c0c0);

    // Add a title to the chart using 18 point Times Bold Itatic font.
    c->addTitle("Test Bar Layer", "timesbi.ttf", 18);

     // Add a bar layer to the chart
    BarLayer * layer = c->addBarLayer();

    // Add a data set to the chart
    layer->addDataSet(DoubleArray(yData, 11));
layer->setXData(DoubleArray(xData, 11));

// Set the x axis scale from -0.5 0.5
c->xAxis()->setLinearScale(-0.5, 0.5);

    // output the chart
    c->makeChart("BarLayer.png");

    //free up resources
    delete c;
    return 0;
}

and attached file is the image I've got.

Could you give me some help to resolve this problem ?

thanks,
Herv
BarLayer.png

  Re: Problem of LinearScale with Bar chart
Posted by Peter Kwan on Nov-10-2008 20:07
Hi Herv?,

The axis scale you want to use probably is not exactly -0.5 to 0.5. If you look at the bar chart, you can see the bars extend to the left of -0.5 and to the right of 0.5 (because the bars have width).

To solve this problem, ChartDirector will automatic add margins to the axis. The amount added is 0.5 unit, which is suitable for "label based" x-axis, but is too large considering you are using an x-axis scale of -0.5 to 0.5 for 11 bars.

To solve the problem, you may use one of the following methods:

(a) Use a label based x-axis, instead of true numeric x-axis. The code is like:

// Add a bar layer to the chart
BarLayer * layer = c->addBarLayer();

// Add a data set to the chart
layer->addDataSet(DoubleArray(yData, 11));

//*** no need to use setXData or setLinearScale

// Set the x axis labels
c->xAxis()->setLabels(DoubleArray(xData, 11));

(b) Disable the "automatic margins" and add your own margins:

.... keep your existing charting code .....

c->xAxis()->setIndent(false);
c->xAixs()->setMargin(30, 30);

Hope this can help.

Regards
Peter Kwan

  Re: Problem of LinearScale with Bar chart
Posted by Herv? Thomy on Nov-10-2008 21:15
Thank you very much.

It works fine now.

Herv