I have just started working with Telerik controls having moved from Infragistics controls.
I have created a 3D pie chart in code-behind whose data is gathered from a database.
The legend items for the chart are also gathered from the database (both the title of each item and its colour is stored in the database as these colours are customisable by the user for each category).
I have managed to set the legend item colours correctly but now I need to set the colours of each 'slice' of the pie chart to match the legend.
I have followed the example on setting custom item styles at http://www.telerik.com/help/wpf/radchart-styling-and-appearance-custom-item-style.html but when I set a breakpoint on the BuildCustomItemStyles method it is only ever trying to style the legend items.
Here is my cod behind (including the BuildCustomItemStyles code).
/// <summary>
/// Interaction logic for AbsenceByReasonChart.xaml
/// </summary>
public
partial
class
AbsenceByReasonChart : UserControl, IWpfChart
{
private
List<OptionPropertiesData> optionPropertiesSet;
/// <summary>
/// Default constructor.
/// </summary>
public
AbsenceByReasonChart()
{
InitializeComponent();
radChart1.
Loaded +=
new
RoutedEventHandler(AbsenceByReasonChart_Loaded);
}
private
void
AbsenceByReasonChart_Loaded(
object
sender, RoutedEventArgs e)
{
WpfZoomUtils.ZoomChartArea(radChart1.DefaultView.ChartArea, Constants.WpfChartZoomFactor);
}
private
Style BuildCustomItemStyle(Control item, Style style, DataPoint point, DataSeries dataSeries)
{
if
((item
as
BaseChartItem3D<Pie3D>) ==
null
)
return
style;
Style newStyle =
new
Style();
newStyle.BasedOn = style;
newStyle.TargetType =
typeof
(Shape);
// this code gets the brush colour
Brush brush =
new
SolidColorBrush(getOptionColour(...));
newStyle.Setters.Add(
new
Setter(Shape.FillProperty, brush));
return
newStyle;
}
public
void
SetChartTitle(DateTime startDate, DateTime endDate)
{
if
(startDate == DateTime.MinValue || startDate ==
null
)
return
;
radChart1.DefaultView.ChartTitle.Content =
string
.Format(
"Absence by Reason {0} to {1}"
, startDate.ToShortDateString(), endDate.ToShortDateString());
radChart1.DefaultView.ChartTitle.HorizontalAlignment = HorizontalAlignment.Center;
}
public
void
PopulateChart(DataTable data, DateTime startDate, DateTime endDate, List<OptionPropertiesData> optionPropertiesList)
{
if
(data ==
null
|| data.Rows.Count == 0 || optionPropertiesList ==
null
|| optionPropertiesList.Count == 0)
return
;
optionPropertiesSet = optionPropertiesList;
if
(radChart1.DefaultView.ChartArea.DataSeries !=
null
&& radChart1.DefaultView.ChartArea.DataSeries.Count > 0)
{
// remove existing data series
radChart1.DefaultView.ChartArea.DataSeries.Clear();
}
// define series data type
DataSeries series =
new
DataSeries()
{
Definition =
new
Pie3DSeriesDefinition(),
LegendLabel =
"Attendance Type"
};
// turn on item tooltips and set the item format of the tooltip for each series item
ISeries3DDefinition chartDefintion = series.Definition
as
Pie3DSeriesDefinition;
if
(chartDefintion !=
null
)
{
chartDefintion.ShowItemToolTips =
true
;
chartDefintion.ItemToolTipFormat =
"#LEGENDLABEL: #Y Employees"
;
chartDefintion.ItemLabelFormat =
"#%{p0}"
;
}
// for the absence by reason chart, the legend colours must match the colours defined for each atendance type
// in the attendance maintenance section
// first, turn OFF the automatic generation of the legend for the chart
radChart1.DefaultView.ChartLegend.UseAutoGeneratedItems =
false
;
// whilst the chart is being populated, set the legend item appropriately
foreach
(DataRow r
in
data.Rows)
{
if
(r ==
null
|| r.ItemArray ==
null
|| r.ItemArray.Length == 0)
continue
;
// the item array of each data row has the following information:
// first item is the absence reason
// second item is the number of employees off due to this reason
// third item is the id of the data option for the absence reason
if
(r.ItemArray[1]
is
int
&& r.ItemArray[0]
is
string
&& r.ItemArray[2]
is
long
)
{
// ignore categories that have no value or rows where the absence reason data option id cannot be found
if
(Convert.ToInt32(r.ItemArray[1]) <= 0 || Convert.ToInt64(r.ItemArray[2]) <= 0)
continue
;
// set the data for this category
series.Add(
new
DataPoint
{
XValue = Convert.ToInt64(r.ItemArray[2]),
YValue = Convert.ToInt32(r.ItemArray[1]),
});
// now define its legend item
ChartLegendItem legendItem =
new
ChartLegendItem()
{
Background =
new
SolidColorBrush(getOptionColour(Convert.ToInt64(r.ItemArray[2]), optionPropertiesList)),
Label = Convert.ToString(r.ItemArray[0])
};
radChart1.DefaultView.ChartLegend.Items.Add(legendItem);
}
}
SetChartTitle(startDate, endDate);
// finally, assign the chart the defined data series
radChart1.DefaultView.ChartArea.DataSeries.Add(series);
}
private
System.Windows.Media.Color getOptionColour(
long
optionID, List<OptionPropertiesData> optionPropertiesList)
{
// find the option properties object for the option with the passed in option id
// and return its associated colour
OptionPropertiesData optionProperty = optionPropertiesList.Find(
delegate
(OptionPropertiesData opData) {
return
opData.getDataOptionID() == optionID; });
if
(optionProperty !=
null
)
{
System.Drawing.Color winFormColour = System.Drawing.Color.FromArgb(optionProperty.getPlannerColour());
if
(winFormColour !=
null
)
{
return
new
System.Windows.Media.Color()
{
A = winFormColour.A,
R = winFormColour.R,
G = winFormColour.G,
B = winFormColour.G
};
}
}
return
new
Color();
}
public
RadChart GetChartInstance()
{
return
radChart1;
}