Skip to main content Skip to footer

OLAP Chart Scaling Tip

When working with ComponentOne OLAP for WinForms, it's very typical to have a view that has too many dimensions to plot nicely on the chart. Unless you define all views for the end-users, there's nothing stopping someone from creating a view which looks like this:

There are so many axis labels that they overlap. Even the chart bars themselves are very tiny and narrow that it's hard to read. One possible solution is to filter the data so that less data is in the view. But what might be best, and easiest, is to configure the chart to efficiently show the data no matter how large or small it may be.

Axis Scaling

A better solution is to automatically display axis scrollbars when there is a large number of items along the x-axis. The C1OlapChart control extends C1Chart, which includes support for axis scaling. We can easily turn on axis scaling and display a scrollbar with 2 lines of code:

c1OlapPage1.OlapChart.ChartArea.AxisX.ScrollBar.Visible = true;  
c1OlapPage1.OlapChart.ChartArea.AxisX.ScrollBar.Scale = 0.5;

This code sets the scale to 0.5 which is 50%. Basically, that means you will see half of the data at one time and can scroll to see the rest.

Setting the scale to 50% is just a guess at what might be the best fit. We can write a little bit of code to dynamically determine the best scale for us depending on the number of data points. That way we get consistent scaling regardless of the data size.

In the UpdateChartScale method below, we count and measure the value labels on the X axis. Then we divide the plot height by the total height of all the labels to determine the best ratio for our scale. If the total height of the labels is less than the plot height, then we can turn off the scaling and scrollbar, because the data would all fit nicely in the view.

private void UpdateChartScale()  
{  
    int plotHeight = c1OlapPage1.OlapChart.ChartArea.PlotArea.Size.Height;  
    if (c1OlapPage1.OlapChart.ChartArea.AxisX.ValueLabels.Count == 0) return;   

    //get graphics to measure height of axis labels  
    Graphics g = c1OlapPage1.CreateGraphics();  
    SizeF labelHeight = g.MeasureString(c1OlapPage1.OlapChart.ChartArea.AxisX.ValueLabels[0].ToString(), c1OlapPage1.OlapChart.Font);  
    int labelCount = c1OlapPage1.OlapChart.ChartArea.AxisX.ValueLabels.Count;  
    double labelHeightTotal = labelHeight.Height * labelCount;  
    if (labelHeightTotal > plotHeight)  
    {  
        //set up scrollbar and scaling  
        c1OlapPage1.OlapChart.ChartArea.AxisX.ScrollBar.Visible = true;  
        c1OlapPage1.OlapChart.ChartArea.AxisX.ScrollBar.Scale = plotHeight / labelHeightTotal;  
    }  
    else  
    {  
        //no need for scaling  
        c1OlapPage1.OlapChart.ChartArea.AxisX.ScrollBar.Visible = false;  
        c1OlapPage1.OlapChart.ChartArea.AxisX.ScrollBar.Scale = 1.0;  
    }  
}

You can call this method whenever the chart's size is changed, or when we need to update the view.

c1OlapPage1.OlapChart.SizeChanged  = new EventHandler(OlapChart_SizeChanged);  

void OlapChart_SizeChanged(object sender, EventArgs e)  
{  
    UpdateChartScale();  
}

You can further customize the scrollbar look and position by setting the ScrollBar's Appearance and Alignment properties. Now, your C1OlapChart will always display a friendly looking plot, no matter how many items your OLAP view contains.

ComponentOne Product Manager Greg Lutz

Greg Lutz

comments powered by Disqus