How to add a ChartPoint that is customized data type?

below is sample source code:

ChartSeries series = new ChartSeries("series", ChartSeriesType.Candle);

series.Points.Add(1, 10, 6, 7, 8);

so there is ChartPoint object add to chart. I defined a new class like below:

class MyChartPoint :  ChartPoint

{

   ...

}

then create a object of  MyChartPoint. let's say the object varaible is myObj. then add to chart like below:

int index = series.Points.Add(myObj);

it works but when i try to get ChartPoint object like below:

MyChartPoint p =  series.Points[index] as MyChartPoint;

but p is null.

so how to add a chart point that is my data 

 



1 Reply

DD Devakumar Dhanapoosanam Syncfusion Team February 28, 2022 02:06 PM UTC

Hi UMLer, 
 
Query: How to add a chart point that is my data 
 
We can add a chart datapoint with customized model using the CharDataBindModel class and set it to the SeriesModel property of series as per the below code example. 
 
public class MyChartPoint 
{ 
    public int XValue { get; set; } 
    public double High { get; set; } 
    public double Low { get; set; } 
    public double Open { get; set; } 
    public double Close { get; set; }

 
    public MyChartPoint(int xValue, double high, double low, double open, double close) 
    { 
        XValue = xValue; 
        High = high; 
        Low = low; 
        Open = open; 
        Close = close; 
    } 
} 
 
private BindingList<MyChartPoint> dataSource;
 

dataSource =
new BindingList<MyChartPoint>(); 
 
dataSource.Add(new MyChartPoint(1, 10, 6, 7, 8)); 
dataSource.Add(new MyChartPoint(2, 10, 6, 8, 7)); 
dataSource.Add(new MyChartPoint(3, 8, 3, 7, 6)); 
dataSource.Add(new MyChartPoint(4, 16, 9, 11, 14)); 
dataSource.Add(new MyChartPoint(5, 18, 11, 13, 16)); 
dataSource.Add(new MyChartPoint(6, 10, 3, 7, 4)); 
dataSource.Add(new MyChartPoint(7, 19, 6, 12, 9)); 
 
ChartDataBindModel dataSeriesModel = new ChartDataBindModel(dataSource); 
dataSeriesModel.XName = "XValue"; 
dataSeriesModel.YNames = new string[] { "High", "Low", "Open", "Close" }; 
 
ChartSeries series = new ChartSeries("Candle"); 
series.Type = ChartSeriesType.Candle; 
series.SeriesModel = dataSeriesModel; 
this.chartControl1.Series.Add(series); 
 
Query: when i try to get ChartPoint object, it returns null. 
 
//now you can get the custom data object directly using the dataSource  
MyChartPoint p = dataSource[index]; 
 
Also, we have prepared a sample and you can download from below link. 
 
 
Please refer the below link for more details and let us know if you need any further assistance on this. 
 
 
Regards, 
Devakumar D 


Loader.
Up arrow icon