#114 Labels and gridlines for DateTime axis missing

open
nobody
5
2015-02-02
2009-06-17
danno
No

After some number of data points are added to a DateTime XAxis, the control stops rendering XAxis labels and gridlines. It turns out that the cause is that the number of tics is calculated incorrectly because XAxis.Scale.MajorUnit is overwritten when the PickScale method calls DateScale.CalcDateStepSize.

The crux of the situation is that I'm using MajorUnit of hours and MajorStep == 24 with MinorUnit of Hours and MinorStep == 4. The ticks render properly on the graph, but the number of tics as calculated by the Scale class is off by an order of magnitude (1 when it should be 7) because my MajorUnit is overwritten by DateScale.CalcDateStepSize when called from Scale.PickScale. It seems that this function respects scale._minorStepAuto, but not scale._majorStepAuto, though I could be misunderstanding what it's supposed to be doing - I'm sure I don't get the full intent since I've only been working with ZedGraph for a short time.

If I make that function respect scale._majorStepAuto so it doesn't overwrite my MajorUnit value when _majorStepAuto == false (like it does in this case because I manually set the step size) then my labels and grid lines show up properly. I have no idea how to qualitatively regression test this change against the rest of ZedGraph though. I'm just going to go with it and see if both my statically rendered graphs and my live UI graphs render properly.

I'd like to submit a bug report and a diff patch for this and another issue I have fixed but I see no activity from admins in the bug tracker. All bugs for at least the last year are unassigned. Anyone have any ideas on how to approach getting fixes into the repo and officially tested with the full code base? I think I'm just going to create bugs and post patches referencing them, but build my own source for now. I'd rather contribute back to the trunk though.

[Test]
public void ZedGraphLabelTest()
{

//zaggy line of data points at Y = 50, spread out on the hour for a week
double[] data = new double[168];
double[] time = new double[168];
ZedGraph.XDate basetime = new ZedGraph.XDate(DateTime.Now);
for (int i = 0; i < data.Length; i++)
{
data[i] = 50.0 + (i % 2);
time[i] = basetime.DateTime.ToOADate();
basetime = basetime.DateTime.AddHours(1.0);
}

renderLineGraph("labeltest.png", createRenderer(time, data));

Assert.AreEqual(168, data.Length);

}

private ZedGraph.Web.ZedGraphWebControlEventHandler createRenderer(double[] x,double[] y)
{
ZedGraph.Web.ZedGraphWebControlEventHandler render = new ZedGraph.Web.ZedGraphWebControlEventHandler(
delegate(ZedGraph.Web.ZedGraphWeb web, System.Drawing.Graphics g, ZedGraph.MasterPane mp)
{
ZedGraph.GraphPane pane = mp.PaneList[0];
pane.Fill = new ZedGraph.Fill(System.Drawing.Color.LightGray);
pane.Legend.Position = ZedGraph.LegendPos.BottomCenter;
pane.Legend.Fill = new ZedGraph.Fill(System.Drawing.SystemColors.ControlDarkDark);
pane.Legend.FontSpec.Fill = pane.Legend.Fill;

pane.Title.Text = "Testing Disappearing Labels";
pane.XAxis.Title.Text = "Time (Hourly points for a week)";
pane.YAxis.Title.Text = "Data";

double min = DateTime.MaxValue.ToOADate();
double max = DateTime.MinValue.ToOADate();
for (int i = 0; i < x.Length;i++ )
{
if (x[i] < min) min = x[i];
if (x[i] > max) max = x[i];
}

ZedGraph.LineItem line = pane.AddCurve("Test Data Set", x, y, System.Drawing.Color.Blue);
line.Tag = "Test Data Set";
line.Symbol.Type = ZedGraph.SymbolType.Diamond;
line.Symbol.Fill = new ZedGraph.Fill(System.Drawing.Color.Blue);
line.Symbol.Size = 3.0f;

pane.Chart.Fill = new ZedGraph.Fill(System.Drawing.SystemColors.ControlDarkDark);

pane.YAxis.MinorGrid.IsVisible = false;
pane.XAxis.MinorGrid.IsVisible = false;
pane.XAxis.MajorGrid.IsVisible = true;
pane.XAxis.MajorGrid.Color = System.Drawing.Color.DarkGreen;
pane.XAxis.MajorGrid.DashOff = 2.0f;
pane.XAxis.MajorGrid.DashOn = 2.0f;
pane.X2Axis.MajorTic.Size = 5.0f;
pane.YAxis.MajorGrid.IsVisible = true;
pane.YAxis.MajorGrid.Color = System.Drawing.Color.DarkGreen;
pane.YAxis.MajorGrid.DashOff = 2.0f;
pane.YAxis.MajorGrid.DashOn = 2.0f;
pane.YAxis.MajorTic.Size = 5.0f;

//assume we're dealing with time-based data
pane.XAxis.Type = ZedGraph.AxisType.Date;
pane.XAxis.Scale.MinorUnit = ZedGraph.DateUnit.Hour;
pane.XAxis.Scale.MajorUnit = ZedGraph.DateUnit.Hour;

pane.XAxis.Scale.MinAuto = false;
pane.XAxis.Scale.MaxAuto = false;
pane.XAxis.Scale.Min = min;
pane.XAxis.Scale.Max = max;
pane.XAxis.Scale.LabelGap = 0.2f;
pane.XAxis.Scale.MinGrace = 0;
pane.XAxis.Scale.MaxGrace = 0;
pane.XAxis.Scale.MajorStep = 24;
pane.XAxis.Scale.MinorStep = 4;
pane.XAxis.Scale.Format = "ddd";
pane.XAxis.Scale.FontSpec.Size = 7.5f;
pane.YAxis.Scale.FontSpec.Size = 8.0f;

pane.LineType = ZedGraph.LineType.Normal;

//if (_watermarkGraphs) addWatermark(pane, 0.87F, -0.15F, -15F);
}
);

return render;
}

private void renderLineGraph(string file, ZedGraph.Web.ZedGraphWebControlEventHandler renderer)
{
if (System.IO.File.Exists(file)) System.IO.File.Delete(file);

ZedGraph.Web.ZedGraphWeb web = new ZedGraph.Web.ZedGraphWeb();
web.RenderMode = ZedGraph.RenderModeType.ImageTag;
web.Height = 450;
web.Width = 1200;
web.IsImageMap = true;

web.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(renderer);
System.IO.MemoryStream image = new System.IO.MemoryStream();
ZedGraph.MasterPane mp = web.CreateGraph(image, System.Drawing.Imaging.ImageFormat.Png);

System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(new System.IO.StringWriter(new StringBuilder()));
web.MakeImageMap(mp, html);

//save the graph
System.IO.File.WriteAllBytes(file, image.ToArray());
}

Attaching a patch file with fix having VERY LIMITED TESTING performed.

Discussion