PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

How to Set Major ticks correctly in Charts for x-axis ?
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3385
Page 1 of 1

Author:  supandi112 [ Mon Jun 27, 2016 8:18 am ]
Post subject:  How to Set Major ticks correctly in Charts for x-axis ?

Hello,

I am creating a chart using the example given in (DefineCharts is called by CreateDocument):
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

However the x-axis in this example are strings, and my data is of type double. I converted the a double array to string array and added it to XSeries of the chart.

The problem is that the x-ticks are displayed for every value in the XSeries, I want to set the x-ticks to something like 2.0 or 10.0, setting a value to chart.XAxis.MajorTick does not have any effect.

Can anyone tell me how to set the Major ticks so that the values on x-axis are readable ?

What I expect to see is values for x which are spaced evenly in steps like 45, 55, 65, 75...... Currently all the values in values_X here displayed along the x-axis.

See the code example:
Code:
// Input data: values_Y and values_X are double arrays
Chart chart = new Chart();
chart.Left = 0;
chart.Width = Unit.FromCentimeter(16);
chart.Height = Unit.FromCentimeter(12);
Series series = chart.SeriesCollection.AddSeries();
series.ChartType = ChartType.Line;

series.Add(values_Y);
XSeries xseries = chart.XValues.AddXSeries();

foreach (var value in values_X)
{
     xseries.Add(value.ToString());
}

chart.XAxis.MajorTick = 10.0;   // Setting the MajorTick here which does not have any effect !
chart.XAxis.MajorTickMark = TickMarkType.Outside;
chart.XAxis.Title.Caption = "X-Axis";


chart.YAxis.MajorTick = 10.0;
chart.YAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.HasMajorGridlines = true;
chart.PlotArea.LineFormat.Color = Colors.DarkGray;
chart.PlotArea.LineFormat.Width = 1;

document.LastSection.Add(chart);



By the way the x values do not start from 0.

Author:  mike29892 [ Wed Jul 13, 2016 11:53 pm ]
Post subject:  Re: How to Set Major ticks correctly in Charts for x-axis ?

Code:
XSeries xseries = chart.XValues.AddXSeries();
string[] xSeriesText = new string[dataSet.Length];
 for (int i = 0; i < dataSet.Length; i++)
{
     xSeriesText[i] = ((i%10)==0) ? i.ToString() : string.Empty;
}
 xseries.Add(xSeriesText);


You need to assign the values for xSeries. The above code is an example where I only show every 10 marks. You can change the 10 to be any number you would like. Note that you must use string.empty for values you do not want to display, "" will still have overlap and potentially hide all of your marks.

Author:  supandi112 [ Fri Jul 15, 2016 12:19 pm ]
Post subject:  Re: How to Set Major ticks correctly in Charts for x-axis ?

thanks, it works.
However it would be good if charts supported double type in x series instead of just strings, so that simply setting the Major ticks would do the job.

Best Regards

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/