我有一个图表,显示相同DateTime X值的两个不同的Y值。我在相同的图表区域使用样条曲线绘制它们,主要和次要Y轴。图表似乎正常工作,但Y轴标签被交换。预期的主Y轴标签位于辅助轴上,反之亦然。
此外,应该注意的是,图表从不在屏幕上显示,它们使用chart.SaveImage
方法保存为文件系统和数据库中的图像文件。
以下是图表输出的图像:
以下是生成图表的代码:
// create the chart
Chart chart = new Chart();
chart.Size = new Size(width, height);
chart.AntiAliasing = AntiAliasingStyles.All;
string datatype = data[0].DataType;
string datatype2 = data2[0].DataType;
SampleUnit unit = GetSampleUnit(data[0].Unit);
string u = "";
if (unit != null)
{
u = unit.ShortName;
}
SampleUnit unit2 = GetSampleUnit(data2[0].Unit);
string u2 = "";
if (unit2 != null)
{
u2 = unit2.ShortName;
}
var chartArea = new ChartArea();
chartArea.AxisX.LabelStyle.Format = "dd MMM\nHH:mm";
chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisX.LabelStyle.Font = new Font("Calibri", 8);
chartArea.AxisY.LabelStyle.Font = new Font("Calibri", 8);
chartArea.AxisY2.LabelStyle.Font = new Font("Calibri", 8);
chartArea.AxisY2.Enabled = AxisEnabled.True;
chartArea.AxisY2.MajorGrid.Enabled = false;
if (string.IsNullOrEmpty(u))
{
chartArea.AxisY.Title = datatype;
}
else
{
chartArea.AxisY.Title = datatype + " (" + u + ")";
}
if (string.IsNullOrEmpty(u2))
{
chartArea.AxisY2.Title = datatype2;
}
else
{
chartArea.AxisY2.Title = datatype2 + " (" + u2 + ")";
}
chartArea.AxisY.TitleForeColor = Color.MediumBlue;
chartArea.AxisY2.TitleForeColor = Color.Red;
chart.ChartAreas.Add(chartArea);
var series = new Series();
series.Name = datatype;
series.ChartType = SeriesChartType.Spline;
series.XValueType = ChartValueType.DateTime;
series.YValueType = ChartValueType.Double;
series.YAxisType = AxisType.Primary;
series.Color = Color.MediumBlue;
chart.Series.Add(series);
var series2 = new Series();
series2.Name = datatype2;
series2.ChartType = SeriesChartType.Spline;
series2.XValueType = ChartValueType.DateTime;
series2.YValueType = ChartValueType.Double;
series.Color = Color.Red;
series.YAxisType = AxisType.Secondary;
chart.Series.Add(series2);
chart.Titles.Add(sensor.Name + " " + char.ToUpper(datatype[0]) + datatype.Substring(1) + " " + char.ToUpper(datatype2[0]) + datatype2.Substring(1));
// bind the datapoints
chart.Series[datatype].Points.DataBindXY(data, "Timestamp", data, "Value");
chart.Series[datatype2].Points.DataBindXY(data2, "Timestamp", data2, "Value");
// draw!
chart.Invalidate();
所以,我有两个问题:
答案 0 :(得分:2)
你有一个剪切和粘贴错字
变化
series.Color = Color.Red;
series.YAxisType = AxisType.Secondary;
到
series2.Color = Color.Red;
series2.YAxisType = AxisType.Secondary;