我也在控件方面苦苦挣扎,直到我意识到您必须以编程方式定义调色板时,我才设法让图表向我展示任何东西。目前我有这个例子,但下周我会在图表上做更多的工作,我可以为你发布一些更好的代码。但这至少可以让你开始!
<Grid Height="520" Grid.Row="1" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="#007EA9" Height="auto" Width="500">
<Border Background="#007EA9">
<telerik:RadCartesianChart x:Name="barChart" Margin="0,6,0,0" Height="250" Width="230">
<telerik:RadCartesianChart.HorizontalAxis>
<telerik:CategoricalAxis LabelFitMode="MultiLine">
<telerik:CategoricalAxis.LabelStyle>
<Style TargetType="TextBlock">
<Setter Property="MaxWidth" Value="10" />
<Setter Property="Margin" Value="4" />
</Style>
</telerik:CategoricalAxis.LabelStyle>
</telerik:CategoricalAxis>
</telerik:RadCartesianChart.HorizontalAxis>
<telerik:RadCartesianChart.VerticalAxis>
<telerik:LinearAxis Maximum="200" Minimum="0">
<telerik:LinearAxis.LabelStyle>
<Style TargetType="TextBlock">
<Setter Property="Width" Value="10" />
<Setter Property="TextAlignment" Value="Right" />
</Style>
</telerik:LinearAxis.LabelStyle>
</telerik:LinearAxis>
</telerik:RadCartesianChart.VerticalAxis>
<telerik:RadCartesianChart.Grid>
<telerik:CartesianChartGrid MajorLinesVisibility="Y" />
</telerik:RadCartesianChart.Grid>
</telerik:RadCartesianChart>
</Border>
</StackPanel>
<StackPanel Grid.Row="1" Height="auto" Width="500" Margin="0,35,0,0">
<Border Background="#007EA9">
<telerik:RadPieChart x:Name="pieChart" Margin="0,0,0,0" Height="250" Width="230" />
</Border>
</StackPanel>
</Grid>
C# 代码
private void CreateSuckyChart()
{
double value;
pieChart.Palette = ChartPalettes.DefaultDark;
barChart.Palette = ChartPalettes.DefaultDark;
var data = new List<WBDataEntry> { new WBDataEntry { Data = "Benchpress", Value = 70 }, new WBDataEntry { Data = "Squats", Value = 15 }, new WBDataEntry { Data = "Lat pulls", Value = 205 }, new WBDataEntry { Data = "Lunges", Value = 55 } };
var series = new BarSeries();
series.CategoryBinding = new PropertyNameDataPointBinding("Date");
series.ValueBinding = new PropertyNameDataPointBinding("Value");
this.barChart.Series.Add(series);
var series2 = new PieSeries();
var labelDefinition = new ChartSeriesLabelDefinition();
labelDefinition.Margin = new Thickness(-10, 0, 0, 0);
labelDefinition.DefaultVisualStyle = this.Resources["PieChartLabelStyle"] as Style;
series2.LabelDefinitions.Add(labelDefinition);
series2.RadiusFactor = 0.7;
series2.AngleRange = new AngleRange(-90, 360);
this.pieChart.Series.Add(series2);
foreach (WBDataEntry indicator in data) // Class WBDataEntry has two properties, Value and Date
{
series2.DataPoints.Add(new PieDataPoint() { Value = indicator.Value, Label = indicator.Data });
series.DataPoints.Add(new CategoricalDataPoint() { Value = indicator.Value , Label = indicator.Data });
}
}