Nevron Forum

How to set different colors for the points in one series

https://www.nevron.com/Forum/Topic5786.aspx

By Xiaolong Zhu - Wednesday, November 2, 2011

 

Hi experts,

I want to create a chart with several point series. To show each series in the legend, i specified different fill styles for the series. Additionally, in each point series, i want to draw the points with different colors. So i also set various colors to the different points. But the result is the points take the series color not the point color. i copied my code here, is there anyone could give any hints? thanks in advance.

//create NPointSeries

public NPointSeries AddPointSeries(IEnumerable<Point> pts, Color seriesColor, string name)

{

NPointSeries series = (NPointSeries)_chart.Series.Add(SeriesType.Point);

series.UseXValues = true;

series.DataLabelStyle.Visible = false;

series.PointShape = PointShape.Ellipse;

series.Name = name;

series.FillStyle = new NColorFillStyle(seriesColor);

foreach(Point pt in pts)

{

NDataPoint dataPoint = new NDataPoint(pt.X, pt.Y);

series.AddDataPoint(dataPoint);

}

UpdatePointSeriesAppearence(series);

return series;

}

// set different colors to each point

private void UpdatePointSeriesAppearence(NPointSeries series)

{

for (int iPt = 0; iPt < series.GetDataPointCount(); iPt++)

{

NDataPoint dataPoint = series.ComposeDataPoint(iPt);

Color color = GetPtColor(iPt);

if (settings.SymbolType == SymbolStyle.None)

{

dataPoint[DataPointValue.StrokeStyle] = new NStrokeStyle(1, color);

dataPoint[DataPointValue.FillStyle] = new NColorFillStyle(Color.Transparent);

}

else

{

dataPoint[DataPointValue.StrokeStyle] = new NStrokeStyle(1, color);

dataPoint[DataPointValue.FillStyle] = new NColorFillStyle(color);

}

}

}

 
By Nevron Support - Thursday, November 3, 2011

Hi Xiaolong,

The problem is that you need to store the composed data point - for example:

   NChart chart = nChartControl1.Charts[0];

   NPointSeries point = new NPointSeries();

   point.Values.Add(10);
   point.Values.Add(20);
   point.Values.Add(30);

   chart.Series.Add(point);

   // using compose
   for (int i = 0; i < point.Values.Count; i++)
   {
    NDataPoint dataPoint = point.ComposeDataPoint(i);

    if (i % 2 == 0)
    {
     dataPoint[DataPointValue.FillStyle] = new NColorFillStyle(Color.Red);
    }
    else
    {
     dataPoint[DataPointValue.FillStyle] = new NColorFillStyle(Color.Green);
    }

// store the data point after you modify it

    point.StoreDataPoint(i, dataPoint);
   }

   nChartControl1.Refresh();

alternatively you can simply use the FillStyles/StrokeStyles collections:

   NChart chart = nChartControl1.Charts[0];

   NPointSeries point = new NPointSeries();

   point.Values.Add(10);
   point.Values.Add(20);
   point.Values.Add(30);

   chart.Series.Add(point);

   // using collections
   for (int i = 0; i < point.Values.Count; i++)
   {
    if (i % 2 == 0)
    {
     point.FillStyles[i] = new NColorFillStyle(Color.Red);
    }
    else
    {
     point.FillStyles[i] = new NColorFillStyle(Color.Green);
    }
   }

   nChartControl1.Refresh();

Hope this helps - let us know if you meet any problems...

 

By Xiaolong Zhu - Monday, November 7, 2011

Thanks, it works now.