Applies to: Nevron Chart for Reporting Services (SSRS 2005 and 2008)
How to color a pie chart in SSRS according to a color in each row of the dataset?
With Nevron Chart for SQL Reporting Services you can specify the color for each pie chart segment based on the colors in your dataset. Let’s say that we have the following dataset:
The background and font colors are specified for each record, and each record represents a sector of the pie chart.
You can achieve pie coloring from the database by having one or more values grouping for the pie chart. The idea is to use these additional groupings label to import the colors specified in the FontColor / BackgroundColor table columns.
Then using some custom code you can easily color the pie slices / labels accordingly. The following code achieves this:
[C#]
using
System;
using
System.Drawing;
using
Nevron.GraphicsCore;
using
Nevron.Chart;
using
Nevron.ReportingServices;
namespace
MyNamespace
{
/// <summary>
/// Sample class
/// </summary>
public
class
MyClass
{
/// <summary>
/// Main entry point
/// </summary>
/// <param name="context"></param>
public
static
void
RSMain(NRSChartCodeContext context)
{
if
(context.Document.Charts.Count == 0)
return
;
NPieChart pieChart = context.Document.Charts[0]
as
NPieChart;
if
(pieChart ==
null
)
return
;
NPieSeries pieSeries = pieChart.Series[0]
as
NPieSeries;
// assign pie color and font color and from the second and third value groupings
int
dpCount = pieSeries.Values.Count / 3;
int
backColorOffset = dpCount;
int
fontColorOffset = dpCount * 2;
for
(
int
i = 0; i < dpCount; i++)
{
Color pieColor = Color.FromName((
string
)pieSeries.Labels[backColorOffset + i]);
pieSeries.FillStyles[i] =
new
NColorFillStyle(pieColor);
pieSeries.BorderStyles[i] =
new
NStrokeStyle(1, pieColor);
NDataLabelStyle dataLabelStyle = (NDataLabelStyle)pieSeries.DataLabelStyle.Clone();
dataLabelStyle.TextStyle.BackplaneStyle.FillStyle =
new
NColorFillStyle(Color.LightGray);
dataLabelStyle.TextStyle.FillStyle =
new
NColorFillStyle(Color.FromName((
string
)pieSeries.Labels[fontColorOffset + i]));
pieSeries.DataLabelStyles[i] = dataLabelStyle;
}
// clean up the chart
for
(
int
i = pieSeries.Values.Count - 1; i >= dpCount; i--)
{
pieSeries.RemoveDataPointAt(i);
}
}
}
}
Article ID: 202, Created On: 9/29/2011, Modified: 9/29/2011