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

Message ListMessage List     Post MessagePost Message

Posted by Yejin on Sep-14-2023 10:59

I'm using "setCols" API. // pLegend.setCols(Chart.AutoGrid);

and I got the result like the image on the right in the attachment.

The image on the left is the result of not applying the API.

Is there any way to allow the legend to fit inside the chart image?

  Re: setCols
Posted by Peter Kwan on Sep-15-2023 04:29
Hi Yejin,

I have just tried myself, and it works normally in my case.

Would you mind to inform me how your code position the legend box? In my case, I put the legend box at (5, 20) with default alignment, which is top-left. This means (5, 20) is the top-left corner of the legend box. In the attached chart image, the top-left corner of the legend box is at (5, 20) as expected.

Also, to use AutoGrid, your code should specify the legend box width. AutoGrid means automatically determine how many columns to use. To do this, ChartDirector needs to know the width available to the legend box. In my test code, I set it to 300.

Below please find my test code. It is modified from the "Stacked Bar Chart" sample code included in the ChartDirector download.

    // The data for the bar chart
    double[] data0 = {100, 115, 165, 107, 67};
    double[] data1 = {85, 106, 129, 161, 123};
    double[] data2 = {67, 87, 86, 167, 157};

    // The labels for the bar chart
    string[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};

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

    // Set default text color to dark grey (0x333333)
    c.setColor(Chart.TextColor, 0x333333);

    // Set the plotarea at (70, 20) and of size 400 x 300 pixels, with transparent background and
    // border and light grey (0xcccccc) horizontal grid lines
    c.setPlotArea(420, 20, 400, 300, Chart.Transparent, -1, Chart.Transparent, 0xcccccc);

    // Add a legend box at (480, 20) using vertical layout and 12pt Arial font. Set background and
    // border to transparent and key icon border to the same as the fill color.
    LegendBox b = c.addLegend(5, 20, true, "Arial", 12);
    b.setBackground(Chart.Transparent, 0x888888);

    // Set the x and y axis stems to transparent and the label font to 12pt Arial
    c.xAxis().setLabelStyle("Arial", 12);
    c.yAxis().setLabelStyle("Arial", 12);

    // Add a stacked bar layer
    BarLayer layer = c.addBarLayer2(Chart.Stack);

    // Add the three data sets to the bar layer
    layer.addDataSet(data0, 0xaaccee, "Server # 1");
    layer.addDataSet(data1, 0xbbdd88, "Server # 2");
    layer.addDataSet(data2, 0xeeaa66, "Server # 3");
    layer.addDataSet(data0, 0xaaccee, "Server # 4");
    layer.addDataSet(data1, 0xbbdd88, "Server # 5");
    layer.addDataSet(data2, 0xeeaa66, "Server # 6");

    // Set the bar border to transparent

    // Enable labelling for the entire bar and use 12pt Arial font
    layer.setAggregateLabelStyle("Arial", 12);

    // Enable labelling for the bar segments and use 12pt Arial font with center alignment
    layer.setDataLabelStyle("Arial", 10).setAlignment(Chart.Center);

    // For a vertical stacked bar with positive data, the first data set is at the bottom. For the
    // legend box, by default, the first entry at the top. We can reverse the legend order to make
    // the legend box consistent with the stacked bar.

    // Set the labels on the x axis.

    // For the automatic y-axis labels, set the minimum spacing to 40 pixels.

    // Add a title to the y axis using dark grey (0x555555) 14pt Arial Bold font
    c.yAxis().setTitle("Y-Axis Title Placeholder", "Arial Bold", 14, 0x555555);

    // Output the chart
    WebChartViewer1.Image = c.makeWebImage(Chart.SVG);

Best Regards
Peter Kwan