I am trying to customize the legend of the chart. So I am building my own ChartLegendItem objects. I am running into a problem however in that I don't know the color of the series associated with the new legend item. I have tried to read the value like this:
ChartControl.InnerChart.DefaultView.ChartArea.DataSeries[i].Definition.Appearance.Fill
However the fill, backgroud, foreground, and strock properites are all null. So I get these white boxes which look crappy. Is there a way for me to read this and get it working right? Any assistance would be greatly appreciate
Thanks in advance
MacKenzie
p.s. Here is the code where I am building the legend
if
(ChartControl.InnerChart != null)
{
if (ChartControl.InnerChart.DefaultView.ChartArea.DataSeries.Count > 0)
{
//Add A legend Item for each unit series
for (int i = 0; i < ChartControl.InnerChart.DefaultView.ChartArea.DataSeries.Count; i++)
{
ChartLegendItem item = new ChartLegendItem() { Label = ChartControl.InnerChart.DefaultView.ChartArea.DataSeries[i].Label,Background = ChartControl.InnerChart.DefaultView.ChartArea.DataSeries[i].Definition.Appearance.Fill };
ChartControl.InnerChart.DefaultView.ChartLegend.Items.Add(item);
}
}
}
7 Answers, 1 is accepted
Unfortunately, there is no easy way to extract the colors of the visual series and use it to manually generate legend items. There is a workaround though. You can set the color of your series using the appearance API exposed by the SeriesDefinition object and set exactly the same color to the legend item for each visual series. Here is a sample code demonstrating how to use the appearance API:
ISeriesDefinition def = new LineSeriesDefinition();
def.Appearance.Fill = new SolidColorBrush(Colors.Yellow);
def.Appearance.Foreground = new SolidColorBrush(Colors.Green);
def.Appearance.Stroke = new SolidColorBrush(Colors.White);
Hope this will help.
Sincerely yours,
Velin
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Could you expand on your code a bit. We are trying something similar.
As mentioned in my previous post, with the current official version of the control there is no easy way to customize series appearance and keep the legend items in sync. In the upcoming version, however, we will introduce a convenient API to do customize the default colors of both the series and the legend using code like this:
RadChart1.PaletteBrushes.Add(new SolidColorBrush(Colors.Green));
This new version is expected later this month.
Sincerely yours,
Velin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
We're having a little trouble setting the PaletteBrushes on the chart, can't find this property on the RadChart control. Can you provide some info? Am I missing something?
#region Usings |
using System.Collections.Generic; |
using Client.CapacityService; |
using Client.Model; |
using Telerik.Windows.Controls; |
using Telerik.Windows.Controls.Charting; |
#endregion |
namespace Client.Views |
{ |
public partial class GraphView |
{ |
private ModelLocator _modelLocator = ModelLocator.Instance(); |
public GraphView() |
{ |
InitializeComponent(); |
DataContext = _modelLocator; //RadChart1.PaletteBrushes <---- No such property |
} |
} |
} |
<telerikChart:RadChart x:Name="RadChart1" ItemsSource="{Binding ProductionLines, Mode=OneWay}"> |
<telerikChart:RadChart.SeriesMappings> |
<telerikCharting:SeriesMapping LegendLabel="Produced"> |
<telerikCharting:SeriesMapping.SeriesDefinition> |
<telerikCharting:StackedBar100SeriesDefinition StackGroupName="Stack1" /> |
</telerikCharting:SeriesMapping.SeriesDefinition> |
<telerikCharting:SeriesMapping.ItemMappings> |
<telerikCharting:ItemMapping FieldName="Produced" DataPointMember="YValue" /> |
<telerikCharting:ItemMapping FieldName="Description" DataPointMember="XCategory" /> |
</telerikCharting:SeriesMapping.ItemMappings> |
</telerikCharting:SeriesMapping> |
<telerikCharting:SeriesMapping LegendLabel="Unused"> |
<telerikCharting:SeriesMapping.SeriesDefinition> |
<telerikCharting:StackedBar100SeriesDefinition StackGroupName="Stack1" /> |
</telerikCharting:SeriesMapping.SeriesDefinition> |
<telerikCharting:SeriesMapping.ItemMappings> |
<telerikCharting:ItemMapping FieldName="UnusedProductionCapcity" DataPointMember="YValue" /> |
<telerikCharting:ItemMapping FieldName="Description" DataPointMember="XCategory" /> |
</telerikCharting:SeriesMapping.ItemMappings> |
</telerikCharting:SeriesMapping> |
<telerikCharting:SeriesMapping LegendLabel="Reduction"> |
<telerikCharting:SeriesMapping.SeriesDefinition> |
<telerikCharting:StackedBar100SeriesDefinition StackGroupName="Stack1" /> |
</telerikCharting:SeriesMapping.SeriesDefinition> |
<telerikCharting:SeriesMapping.ItemMappings> |
<telerikCharting:ItemMapping FieldName="DueToError" DataPointMember="YValue" /> |
<telerikCharting:ItemMapping FieldName="Description" DataPointMember="XCategory" /> |
</telerikCharting:SeriesMapping.ItemMappings> |
</telerikCharting:SeriesMapping> |
</telerikChart:RadChart.SeriesMappings> |
</telerikChart:RadChart> |
How do the PaletteBrushes stay in color sync with SeriesMappings? In our code, we are still showing discrepancies with asynchronous calls to data. Whichever values return first is assigned the first color in PaletteBrushes rather than based on SeriesMappings (...and it must be color coded accurately).
// Success is color coded to green. |
SolidColorBrush greenBrush = new SolidColorBrush(); |
greenBrush.Color = Color.FromArgb(255, 19, 130, 26); |
// Error is color coded to red. |
SolidColorBrush redBrush = new SolidColorBrush(); |
redBrush.Color = Color.FromArgb(255, 255, 29, 0); |
feedsChart.PaletteBrushes.Add(greenBrush); |
feedsChart.PaletteBrushes.Add(redBrush); |
// Success SeriesMapping |
SeriesMapping successSeriesMapping = new SeriesMapping(); |
... |
feedsChart.SeriesMappings.Add(goodSeriesMapping); |
// Error SeriesMapping |
SeriesMapping errorSeriesMapping = new SeriesMapping(); |
... |
feedsChart.SeriesMappings.Add(errorSeriesMapping); |
The current implementation of the PaletteBrushes property applies the colors to the series in the order they come from the items source. You should sync this order manually in order to be sure that every time a visual series will get the correct brush applied.
Regards,
Velin
the Telerik team