Question

I am using Chart control to display my network statistics (download, upload):

chart1.Titles.Add("Test Chart");

Series seriesDownload = new Series("KB/s");
seriesDownload.Color = Color.DarkBlue;
seriesDownload.ChartType = SeriesChartType.Spline;
seriesDownload.BorderWidth = 2;
chart1.Series.Add(seriesDownload);

Series seriesPps = new Series("pps");
seriesPps.Color = Color.Black;
seriesPps.ChartType = SeriesChartType.Spline;
seriesPps.BorderWidth = 2;
chart1.Series.Add(seriesPps);

Is it possible to add text near each line in order to distinguish between both colors ?

enter image description here

private void chartTimer_Tick(object sender, EventArgs e)
{
    chart1.Series[1].LegendText = chart1.Series[1].Name = str + " KB/s";
    DataPoint Point = chart1.Series[1].Points[chart1.Series[1].Points.Count - 1];
    Point.Label = chart1.Series[1].Name;

    DataPoint _point = default(DataPoint);
    foreach (DataPoint item in chart1.Series[1].Points)
    {
       item.Label = "";
       item.MarkerStyle = MarkerStyle.None;
    }
}
Was it helpful?

Solution

Something like this will add a label with the series name to the last point on your series. This is VB.NET but you should be able to parse it to C#

'remove all previous datapoint labels
Dim _point As DataPoint
For Each _point In Chart1.Series(i).Points
    _point.Label = ""
    _point.MarkerStyle = MarkerStyle.None
Next


'add label to last point
Dim Point As DataPoint = Chart1.Series(i).Points(Chart1.Series(i).Points.Count - 1)
Point.Label = Chart1.Series(i).Name
Point.MarkerStyle = MarkerStyle.Circle

enter image description here

Or are you looking to populate the legend with the series name?? then

Chart1.Series(i).LegendText = Chart1.Series(i).Name

EDIT based on the askee submitted code in C#

private void chartTimer_Tick(object sender, EventArgs e)
{

    foreach (DataPoint item in chart1.Series[1].Points)
    {
        item.Label = "";
    }

    chart1.Series[1].LegendText = chart1.Series[1].Name = str + " KB/s";
    DataPoint Point = chart1.Series[1].Points[chart1.Series[1].Points.Count - 1];
    Point.Label = chart1.Series[1].Name;

}

OTHER TIPS

Changing the name of the series is not good practice, as the SeriesCollection can be indexed by name (e.g., chart1.Series["MySeries"]) which might then fail after the name change.

If you're trying to add a static label at the end of the code, you can do it as a smart label. Look into the "#VALX#", "#VAL#" modifiers to use in labels:

DataPoint Point = chart1.Series[1].Points[chart1.Series[1].Points.Count - 1];
Point.Label = "#VAL" + " kB/s";

which will automatically add the current x or y value to the label. See http://msdn.microsoft.com/en-us/library/dd456687(v=vs.110).aspx

As an aside, it's very helpful to put the names of your Series into const variables in the class (or static variables in another class), so you can index them without fear of typos. Makes your code much easier to understand, too.

private const string _downloadSeries = "download";

// in some initialization method
Series seriesDownload = new Series(_downloadSeries);
chart1.Series.Add(seriesDownload);

// Access the series
DataPoint point = chart1.Series[_downloadSeries].Points[0];

for example.

EDIT: If you just want to distinguish the two colors, that's exactly what a legend is for. If you want to show the latest value as well, then this code will do that

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top