我正在开发一个显示记录数据的应用程序。此应用程序具有包含多个数据系列的图形元素。每个都用一个单独的Y标度绘制(为什么他们想要这对我没有意义,但我使用Microsoft Charting示例应用程序中的示例实现它。我正在使用的代码可以在下面看到:
public void CreateYAxis(Chart chart, ChartArea area, System.Windows.Forms.DataVisualization.Charting.Series series, float axisOffset, float labelsSize)
{
ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
areaSeries.AxisX.MajorGrid.Enabled = true;
areaSeries.AxisX.MajorTickMark.Enabled = true;
areaSeries.AxisX.LabelStyle.Enabled = true;
areaSeries.AxisY.MajorGrid.Enabled = true;
areaSeries.AxisY.MajorTickMark.Enabled = true;
areaSeries.AxisY.LabelStyle.Enabled = true ;
areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
series.ChartArea = areaSeries.Name;
// Create new chart area for axis
ChartArea areaAxis = chart.ChartAreas.Add("AxisY_" + series.ChartArea);
areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());
// Create a copy of specified series
System.Windows.Forms.DataVisualization.Charting.Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
seriesCopy.ChartType = series.ChartType;
foreach (System.Windows.Forms.DataVisualization.Charting.DataPoint point in series.Points)
{
seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
}
// Hide copied series
seriesCopy.IsVisibleInLegend = false;
seriesCopy.ChartArea = areaAxis.Name;
// Disable grid lines & tickmarks
areaAxis.AxisX.LineWidth = 0;
areaAxis.AxisX.MajorGrid.Enabled = true;
areaAxis.AxisX.MajorTickMark.Enabled = true;
areaAxis.AxisX.LabelStyle.Enabled = true;
areaAxis.AxisY.MajorGrid.Enabled = false;
areaAxis.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
areaAxis.CursorX.Interval = .00001;
areaAxis.AxisX.ScaleView.Zoom(0, 2000);
}
此代码可以正确生成具有不同比例的多个图形。用户还可以选择图表,并生成可以左右药物的垂直红线。我想用它来选择和显示所选位置的Y值。但是我试图实现这个有两个问题。
1。)即使我在创建图表区域之前实现了以下代码,我的图例仍会显示在我的图表中:
MyWinformChart.Legends.Add("MyLegend");
// Set legend style
MyWinformChart.Legends["MyLegend"].LegendStyle = LegendStyle.Table;
// Set legend docking
MyWinformChart.Legends["MyLegend"].Docking = Docking.Bottom;
MyWinformChart.Legends["MyLegend"].IsDockedInsideChartArea = false;
2.。)有什么方法可以显示图例中所选位置的Y值?基本上,如果他们正在查看多个变量并单击图中的某个位置,我希望图例显示这些变量的Y值。我无法在线或在MSDN文档中找到此示例。
谢谢!