编程语言
首页 > 编程语言> > WPF编程,telerik控件柱状图根据数值不同,显示不同颜色的一种方法。

WPF编程,telerik控件柱状图根据数值不同,显示不同颜色的一种方法。

作者:互联网

此方法仅适用于telerik的控件,且是版本较早的。此处只用于做一个参考事例,直接复制不保证可运行。

1、前台

        <telerikChart:RadChart x:Name="radChart"
                               Grid.Row="1"
                               d:LayoutOverrides="Width, Height">
            <telerikChart:RadChart.DefaultView>
                <telerikChart:ChartDefaultView>
                    <telerikChart:ChartDefaultView.ChartTitle>
                        <telerikChart:ChartTitle Content="标题"
                                                 HorizontalAlignment="Stretch" />
                    </telerikChart:ChartDefaultView.ChartTitle>
                </telerikChart:ChartDefaultView>
            </telerikChart:RadChart.DefaultView>
        </telerikChart:RadChart>

 

2、后台

鼠标事件:

        private void Button_Click4(object sender, RoutedEventArgs e)
        {
            int maxItems = 30;
            Random r = new Random();

            List<Company> sampleData = new List<Company>();

            for (int i = 0; i < maxItems; i++)
            {
                sampleData.Add(new Company(r.Next(200, 400)));
            }

            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.LegendLabel = "My Custom Bars";
            seriesMapping.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping.ItemMappings.Add(new ItemMapping("PurchasePrice", DataPointMember.YValue));

            radChart.ItemsSource = sampleData;
            radChart.SeriesMappings.Add(seriesMapping);

            radChart.CreateItemStyleDelegate = BuildCustomItemStyle;

        }

 颜色判定事件:

        public Style BuildCustomItemStyle(Control item, Style style, DataPoint point, DataSeries dataSeries)
        {
            Style newStyle = new Style(style.TargetType);
            newStyle.BasedOn = style;
            Brush brush;

            if (item is BaseChartItem)
            {
                if (dataSeries[(item as BaseChartItem).CurrentIndex].YValue > 300)
                {
                    brush = new SolidColorBrush(Colors.Red);
                }
                else
                {
                    brush = new SolidColorBrush(Colors.Green);
                }
                newStyle.Setters.Add(new Setter(Shape.FillProperty, brush));
            }
            //用于其它类型的图表判断
            //if (item is SeriesItemLabel)
            //{
            //    if ((item as SeriesItemLabel).DataPoint.YValue > 300)
            //    {
            //        brush = new SolidColorBrush(Colors.Red);
            //    }
            //    else
            //    {
            //        brush = new SolidColorBrush(Colors.Green);
            //    }
            //    newStyle.Setters.Add(new Setter(SeriesItemLabel.FillProperty, brush));
            //    newStyle.Setters.Add(new Setter(SeriesItemLabel.StrokeProperty, brush));
            //}
            //if (item is ChartLegendItem)
            //{
            //    brush = this.Resources["LegendItemStyle"] as Brush;
            //    newStyle.Setters.Add(new Setter(System.Windows.Shapes.Path.FillProperty, brush));
            //}

            return newStyle;
        }

 

 

 

 

 

 

标签:控件,seriesMapping,telerik,Add,item,柱状图,brush,new,newStyle
来源: https://blog.csdn.net/qq_43307934/article/details/95193085