无法将标签应用于datapoint

时间:2015-07-27 16:38:51

标签: c# charts

我的Windows窗体应用程序中有一个图表控件,它显示了四个系列的各种数据输入。

图表工作到目前为止很好,但最近似乎遇到了一些问题,我无法确定原因。

首先,我尝试更改系列类型,以便其中一个系列类型为“FastPoint”,另外两个类型为“Point”类型。尽管在表单和代码中的属性框中更改了此内容,但由于某些原因这种情况并未发生。

这是一个不重要的小问题,但问题在于,一个系列意味着每个点都标有Y轴和X轴的值,将它放在图表上。直到最近这个工作正常,但由于某种原因,标签似乎没有出现。这是应该允许每个点在图表上标记的代码:

        //so long as textbox is not blank
        if (!((String.IsNullOrEmpty(txtKetAdd.Text)) || String.IsNullOrWhiteSpace(txtKetAdd.Text)))
        {
            int minutesElapsed = StopWatchM.Elapsed.Minutes;   //truncated minutes value for x-axis
            KetAdditions.Add(Convert.ToDouble(txtKetAdd.Text)); //user entered ket dose for y-axis

            chartBP.ChartAreas[0].AxisY2.Title = "Ketamine Dose Added (mg)"; //title for right-hand Y axis (scale for ket dose)

            //with each button click, add last (newest) value on list to y-coordinate on new point in series
            chartBP.Series["Ketamine Additions"].Points.AddXY(minutesElapsed, KetAdditions.Last());

            //label last point in list as it is added to graph
            chartBP.Series["Ketamine Additions"].Points.Last().Label = KetAdditions.Last().ToString() + " (mg)"
                                                                        + minutesElapsed.ToString() + " mins";

            //set data series appearance for when values are added
            //display points and no lines            
            chartBP.Series["Ketamine Additions"].ChartType = SeriesChartType.FastPoint;

            txtKetAdd.Clear();
        }

如果有人能发现问题,那将非常感谢!

有关图表的其他代码如下所示

        //from InitializeComponent()
        //chart area initialised
        //give chart title
        Title BPtitle = new Title();
        BPtitle.Name = "BPvisual";
        BPtitle.Text = "Patient Blood Pressure Visual";
        chartBP.Titles.Add(BPtitle);
        //enable both Y axes
        chartBP.Series[0].YAxisType = AxisType.Primary;
        chartBP.Series[1].YAxisType = AxisType.Secondary;
        //format ketamine series to mould to BP series chart
        chartBP.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
        chartBP.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
        chartBP.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
        chartBP.ChartAreas[0].AxisY2.IsStartedFromZero = chartBP.ChartAreas[0].AxisY.IsStartedFromZero;
        //label chart axis
        chartBP.ChartAreas[0].AxisX.Title = "Time Elapsed (Minutes)";    //title for adjusting bottom axis
        chartBP.ChartAreas[0].AxisY.Title = "Blood Pressure (mmHg)";     //title for left-hand Y axis (scale for blood pressure)
        //set colours for chart lines and Legend
        chartBP.Series["Blood Pressure"].ChartType = SeriesChartType.Line;
        chartBP.Series["Blood Pressure"].Color = Color.Blue;
        chartBP.Series["Ketamine Additions"].ChartType = SeriesChartType.Point;
        chartBP.Series["Ketamine Additions"].Color = Color.Red;
        chartBP.Series["Systolic Pressure"].ChartType = SeriesChartType.FastPoint;
        chartBP.Series["Systolic Pressure"].Color = Color.Orange;
        chartBP.Series["Diastolic Pressure"].ChartType = SeriesChartType.FastPoint;
        chartBP.Series["Diastolic Pressure"].Color = Color.Green;
        //set chart location & dimensions
        chartBP.ChartAreas[0].Position.X = 3;
        chartBP.ChartAreas[0].Position.Y = 7;
        chartBP.ChartAreas[0].Position.Height = 94;
        chartBP.ChartAreas[0].Position.Width = 65;
        chartBP.ChartAreas[0].AxisX.IsStartedFromZero = true;
        chartBP.ChartAreas[0].AxisY.IsStartedFromZero = false;

UpdateChart()函数:         public void updateChart(int minutesElapsed)         {

        chartBP.Text = "Blood Pressure Visual";
        //Blood Pressure
        //Create correct coordinate using reading and time taken
        chartBP.Series["Blood Pressure"].Points.AddXY(minutesElapsed, BPlist.Last()); //add point to series at these coordinates
        if (BPlist.Last() == BPlist.First()) //if the point added is first in series...
        {
            chartBP.Series["Blood Pressure"].Points.First().Label = "[Op Start]"; //apply Op Start label to point
        }

        //Systolic Pressure
        chartBP.Series["Systolic Pressure"].Points.AddXY(minutesElapsed, SystolList.Last()); //add point to series
        //Diastolic Pressure
        chartBP.Series["Diastolic Pressure"].Points.AddXY(minutesElapsed, DiastolList.Last()); //add point to series

        //specify line graph and colour of line
        //set chart format to connected dots
        //Blood Pressure
        chartBP.Series["Blood Pressure"].ChartType = SeriesChartType.Line;

        if (chartBP.Series["Blood Pressure"].Points.Count > 0) //as long as there is minimum 1 point on chart...
        {
            btnOpEnd.Enabled = true; //reading can be marked as end of operation
        }
    }

1 个答案:

答案 0 :(得分:0)

遍历您感兴趣的系列中的Points,然后为点启用IsValueShownAsLabel

foreach (DataPoint dp in series.Points)

{

       dp.IsValueShownAsLabel = true;

}