Hello In my wpf application my requirement is to plot a two line graph with different color of point markers..
i achived colours for point marker foor single line graph. but if try that method for two line graph am not getting result. am getting single line only,,
Am getting double line if i use dataseries . but i need to do that by item series,,,
My values are from single tabel, two different columns....
here is my code...
public void chartmain()
{
try
{
var qry = (from s in SvarkWindow.BPList
where (s.User_id.Equals(1))
orderby s.Test_date ascending
select new { s.BP_Score, s.BP_Score_Dia, s.Test_date }).ToList();
if (qry.Count == 0)
{
SvarkMessageBox.MessageBox.ShowInformation("No Records Found...");
radChart.Visibility = System.Windows.Visibility.Hidden;
//listStackPanel.Visibility = System.Windows.Visibility.Hidden;
}
else
{
radChart.Visibility = System.Windows.Visibility.Visible;
List<BPChartHelper> HelperData = new List<BPChartHelper>();
List<BPChartHelper> HelperData1 = new List<BPChartHelper>();
foreach (var item in qry)
{
SvarkMessageBox.MessageBox.ShowInformation(item.ToString());
//lineSeries1.Add(new DataPoint() { YValue = Convert.ToDouble(item.BP_Score), XValue = item.Test_date.ToOADate() });
//lineSeries2.Add(new DataPoint() { YValue = Convert.ToDouble(item.BP_Score_Dia), XValue = item.Test_date.ToOADate() });
//valueList.Add(new KeyValuePair<DateTime, decimal>(item.Test_date.Date, item.HB_score));
HelperData.Add(new BPChartHelper(item.Test_date, item.BP_Score));
HelperData1.Add(new BPChartHelper(item.Test_date, item.BP_Score_Dia));
}
SeriesMapping mapping = new SeriesMapping();
mapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
mapping.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
radChart.ItemsSource = HelperData;
radChart.SeriesMappings.Add(mapping);
radChart.DefaultView.ChartArea.AxisX.IsDateTime = true;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Auto;
radChart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
radChart.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd-MMM";
LineSeriesDefinition line = new LineSeriesDefinition();
radChart.DefaultSeriesDefinition = line;
line.ShowPointMarks = true;
line.ShowItemLabels = true;
line.Appearance.Stroke = new SolidColorBrush(Colors.DarkGreen);
line.Appearance.PointMark.Stroke = new SolidColorBrush(Colors.Black);
radChart.DefaultSeriesDefinition.PointMarkItemStyle = this.Resources["MyPointMark_Style"] as Style;
radChart.DefaultSeriesDefinition.SeriesItemLabelStyle = this.Resources["MySeriesItemLabel_Style"] as Style;
//**********************
SeriesMapping mapping1 = new SeriesMapping();
mapping1.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
mapping1.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
radChart.ItemsSource = HelperData1;
radChart.SeriesMappings.Add(mapping1);
radChart.DefaultView.ChartArea.AxisX.IsDateTime = true;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Auto;
radChart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
radChart.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd-MMM";
LineSeriesDefinition line1 = new LineSeriesDefinition();
radChart.DefaultSeriesDefinition = line1;
line1.ShowPointMarks = true;
line1.ShowItemLabels = true;
line1.Appearance.Stroke = new SolidColorBrush(Colors.DarkBlue);
line1.Appearance.PointMark.Stroke = new SolidColorBrush(Colors.Red);
radChart.DefaultSeriesDefinition.PointMarkItemStyle = this.Resources["MyPointMark_Style"] as Style;
radChart.DefaultSeriesDefinition.SeriesItemLabelStyle = this.Resources["MySeriesItemLabel_Style"] as Style;
this.radChart.DefaultView.ChartLegend.Visibility = System.Windows.Visibility.Visible;
//**************
radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = false;
ChartLegendItem item1 = new ChartLegendItem();
item1.Label = "BP SYS";
item1.MarkerFill = new SolidColorBrush(Colors.DarkGreen);
radChart.DefaultView.ChartLegend.Items.Add(item1);
ChartLegendItem item2 = new ChartLegendItem();
item2.Label = "BP DIA";
item2.MarkerFill = new SolidColorBrush(Colors.SkyBlue);
radChart.DefaultView.ChartLegend.Items.Add(item2);
radChart.DefaultView.ChartLegend.Header = "BP Results";
this.radChart.DefaultView.ChartLegend.LegendItemMarkerShape = MarkerShape.StarFiveRay;
//**************
// this.BPGridView.ItemsSource = qry;
// this.radChart.DefaultView.ChartArea.DataSeries.Add(lineSeries1);
// this.radChart.DefaultView.ChartArea.DataSeries.Add(lineSeries2);
}
}
And My Helper Class.....
class BPChartHelper : INotifyPropertyChanged
{
private DateTime _date;
private SolidColorBrush _pointMarkFill;
private decimal _yvalue;
public BPChartHelper(DateTime date, decimal yvalue)
{
this._date = date;
this._yvalue = yvalue;
this.UpdatePointMarkVisibility();
}
public event PropertyChangedEventHandler PropertyChanged;
public DateTime Date
{
get
{
return _date;
}
set
{
if (this._date == value)
return;
this._date = value;
this.OnPropertyChanged("Date");
}
}
public decimal YValue
{
get
{
return _yvalue;
}
set
{
if (this._yvalue == value)
return;
this._yvalue = value;
this.OnPropertyChanged("YValue");
}
}
public SolidColorBrush PointMarkFill
{
get
{
return _pointMarkFill;
}
private set
{
if (object.Equals(this._pointMarkFill, value))
return;
this._pointMarkFill = value;
this.OnPropertyChanged("PointMarkVisibility");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void UpdatePointMarkVisibility()
{
if (this.YValue <=100)
this.PointMarkFill = new SolidColorBrush(Colors.DarkGreen);
else if(this.YValue<=105 && this.YValue>=100)
this.PointMarkFill = new SolidColorBrush(Colors.DarkBlue);
else if(this.YValue>=105 &&this.YValue<=120)
this.PointMarkFill = new SolidColorBrush(Colors.Pink);
else if (this.YValue >= 120 && this.YValue <= 125)
this.PointMarkFill = new SolidColorBrush(Colors.Orange);
else if (this.YValue >= 125 && this.YValue <= 130)
this.PointMarkFill = new SolidColorBrush(Colors.Red);
}
}
}
i achived colours for point marker foor single line graph. but if try that method for two line graph am not getting result. am getting single line only,,
Am getting double line if i use dataseries . but i need to do that by item series,,,
My values are from single tabel, two different columns....
here is my code...
public void chartmain()
{
try
{
var qry = (from s in SvarkWindow.BPList
where (s.User_id.Equals(1))
orderby s.Test_date ascending
select new { s.BP_Score, s.BP_Score_Dia, s.Test_date }).ToList();
if (qry.Count == 0)
{
SvarkMessageBox.MessageBox.ShowInformation("No Records Found...");
radChart.Visibility = System.Windows.Visibility.Hidden;
//listStackPanel.Visibility = System.Windows.Visibility.Hidden;
}
else
{
radChart.Visibility = System.Windows.Visibility.Visible;
List<BPChartHelper> HelperData = new List<BPChartHelper>();
List<BPChartHelper> HelperData1 = new List<BPChartHelper>();
foreach (var item in qry)
{
SvarkMessageBox.MessageBox.ShowInformation(item.ToString());
//lineSeries1.Add(new DataPoint() { YValue = Convert.ToDouble(item.BP_Score), XValue = item.Test_date.ToOADate() });
//lineSeries2.Add(new DataPoint() { YValue = Convert.ToDouble(item.BP_Score_Dia), XValue = item.Test_date.ToOADate() });
//valueList.Add(new KeyValuePair<DateTime, decimal>(item.Test_date.Date, item.HB_score));
HelperData.Add(new BPChartHelper(item.Test_date, item.BP_Score));
HelperData1.Add(new BPChartHelper(item.Test_date, item.BP_Score_Dia));
}
SeriesMapping mapping = new SeriesMapping();
mapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
mapping.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
radChart.ItemsSource = HelperData;
radChart.SeriesMappings.Add(mapping);
radChart.DefaultView.ChartArea.AxisX.IsDateTime = true;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Auto;
radChart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
radChart.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd-MMM";
LineSeriesDefinition line = new LineSeriesDefinition();
radChart.DefaultSeriesDefinition = line;
line.ShowPointMarks = true;
line.ShowItemLabels = true;
line.Appearance.Stroke = new SolidColorBrush(Colors.DarkGreen);
line.Appearance.PointMark.Stroke = new SolidColorBrush(Colors.Black);
radChart.DefaultSeriesDefinition.PointMarkItemStyle = this.Resources["MyPointMark_Style"] as Style;
radChart.DefaultSeriesDefinition.SeriesItemLabelStyle = this.Resources["MySeriesItemLabel_Style"] as Style;
//**********************
SeriesMapping mapping1 = new SeriesMapping();
mapping1.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
mapping1.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
radChart.ItemsSource = HelperData1;
radChart.SeriesMappings.Add(mapping1);
radChart.DefaultView.ChartArea.AxisX.IsDateTime = true;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Auto;
radChart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
radChart.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd-MMM";
LineSeriesDefinition line1 = new LineSeriesDefinition();
radChart.DefaultSeriesDefinition = line1;
line1.ShowPointMarks = true;
line1.ShowItemLabels = true;
line1.Appearance.Stroke = new SolidColorBrush(Colors.DarkBlue);
line1.Appearance.PointMark.Stroke = new SolidColorBrush(Colors.Red);
radChart.DefaultSeriesDefinition.PointMarkItemStyle = this.Resources["MyPointMark_Style"] as Style;
radChart.DefaultSeriesDefinition.SeriesItemLabelStyle = this.Resources["MySeriesItemLabel_Style"] as Style;
this.radChart.DefaultView.ChartLegend.Visibility = System.Windows.Visibility.Visible;
//**************
radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = false;
ChartLegendItem item1 = new ChartLegendItem();
item1.Label = "BP SYS";
item1.MarkerFill = new SolidColorBrush(Colors.DarkGreen);
radChart.DefaultView.ChartLegend.Items.Add(item1);
ChartLegendItem item2 = new ChartLegendItem();
item2.Label = "BP DIA";
item2.MarkerFill = new SolidColorBrush(Colors.SkyBlue);
radChart.DefaultView.ChartLegend.Items.Add(item2);
radChart.DefaultView.ChartLegend.Header = "BP Results";
this.radChart.DefaultView.ChartLegend.LegendItemMarkerShape = MarkerShape.StarFiveRay;
//**************
// this.BPGridView.ItemsSource = qry;
// this.radChart.DefaultView.ChartArea.DataSeries.Add(lineSeries1);
// this.radChart.DefaultView.ChartArea.DataSeries.Add(lineSeries2);
}
}
And My Helper Class.....
class BPChartHelper : INotifyPropertyChanged
{
private DateTime _date;
private SolidColorBrush _pointMarkFill;
private decimal _yvalue;
public BPChartHelper(DateTime date, decimal yvalue)
{
this._date = date;
this._yvalue = yvalue;
this.UpdatePointMarkVisibility();
}
public event PropertyChangedEventHandler PropertyChanged;
public DateTime Date
{
get
{
return _date;
}
set
{
if (this._date == value)
return;
this._date = value;
this.OnPropertyChanged("Date");
}
}
public decimal YValue
{
get
{
return _yvalue;
}
set
{
if (this._yvalue == value)
return;
this._yvalue = value;
this.OnPropertyChanged("YValue");
}
}
public SolidColorBrush PointMarkFill
{
get
{
return _pointMarkFill;
}
private set
{
if (object.Equals(this._pointMarkFill, value))
return;
this._pointMarkFill = value;
this.OnPropertyChanged("PointMarkVisibility");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void UpdatePointMarkVisibility()
{
if (this.YValue <=100)
this.PointMarkFill = new SolidColorBrush(Colors.DarkGreen);
else if(this.YValue<=105 && this.YValue>=100)
this.PointMarkFill = new SolidColorBrush(Colors.DarkBlue);
else if(this.YValue>=105 &&this.YValue<=120)
this.PointMarkFill = new SolidColorBrush(Colors.Pink);
else if (this.YValue >= 120 && this.YValue <= 125)
this.PointMarkFill = new SolidColorBrush(Colors.Orange);
else if (this.YValue >= 125 && this.YValue <= 130)
this.PointMarkFill = new SolidColorBrush(Colors.Red);
}
}
}