Autor: Libor Bešenyi
Dátum: 5.9.2011
We needed to shown in Dundas chart Y-Axis just values used as data points in chart. We found collection in AxisY area’s property – but there was a problem with setting value. There is needed to set middle value which we need, for instance, if we have displayed point zero, there will be needed to set begin and point to -1, 1.
Similar problem occurs for followed value(s) – there is needed to calculate borders with this formula: LastValue * 2, (CurrentValue – LastValue) * 2, for determination new "middle" value.
So final code can be:
private void button1_Click(object sender, EventArgs e)
{
chart1.ChartAreas.Clear();
chart1.Series.Clear();
chart1.ChartAreas.Add(new ChartArea(MainChartAreaName));
chart1.Series.Add(new Series(MainSeriesName) { ChartType = SeriesChartType.Line } );
AddPoints(0.83, 1.50, 1.40, 2.00, 1.40, 1.70);
}
private void AddPoints(params double[] values)
{
var area = chart1.ChartAreas[0];
area.AxisY.Minimum = 0.0;
area.AxisY.Maximum = values.Max();
// Add points
var series = chart1.Series[MainSeriesName];
foreach (double value in values)
series.Points.Add(value);
// Prepare Y axis
double lastValue = 0.0;
foreach (double value in values.Distinct().OrderBy(item => item))
{
var label = area.AxisY.CustomLabels.Add(lastValue * 2, (value - lastValue) * 2, value.ToString());
label.GridTicks = GridTickTypes.All;
lastValue = value;
} // foreach
}