DevExpress chart 的序列的颜色动态变更
前几天需要一个新的需求。
在系统的首页中需要显示产品中分类和小分类的统计信息。
中分类使用的饼状图,小分类使用了柱状图。
现在要求 点击中分类后显示对应小分类的统计信息,而且要求小分类的柱状图的颜色和点击的中分类一致。
如下图
基本实现思路,如下
- 在中分类的点击事件中获取当前点击的中分类
- 生产一个自定义的PaletteEntry,其颜色和 “当前点击的中分类”一致
- 自定义小分类的PaletteRepository
- 将小分类的PaletteName 设置为自定义的Palette
下面就是如何实现啦。(代码)
private void chartItemMSaleSumAmt_ObjectSelected(object sender, DevExpress.XtraCharts.HotTrackEventArgs e)
{
if (e.AdditionalObject == null) return;
SeriesPoint point = (SeriesPoint)e.AdditionalObject;//当天点击的中分类
//只让当前点击的中分类从饼图中展开
(chartItemMSaleSumAmt.Series[0].View as PieSeriesView).ExplodedPoints.Clear();
(chartItemMSaleSumAmt.Series[0].View as PieSeriesView).ExplodedPoints.Add(point);
//之前绑定中分类chart 的时候,将数据源绑定给Series[0].Tag(由于SeriesPoint能够绑定的中分类的属性是有限的。)
List<ItemMSaleSumAmtDto> listMItem = chartItemMSaleSumAmt.Series[0].Tag as List<ItemMSaleSumAmtDto>;
if (listMItem == null)return;
//从中分类的数据源中查找当前点击的中分类对象。
ItemMSaleSumAmtDto selectItemMSaleSumAmtDto = listMItem.Find(a => a.ItemMName == point.Argument);
if (selectItemMSaleSumAmtDto == null)
return;
//获取点击的中分类的PaletteEntry
// DevExpress.XtraCharts.Palettes.Default 是中分类使用的Palette
int itemMPaletteEntryIndex = point.SeriesPointID % DevExpress.XtraCharts.Palettes.Default.Count;
PaletteEntry paletteEntry = DevExpress.XtraCharts.Palettes.Default[itemMPaletteEntryIndex];
//自定义中分类的Palette
Palette ItemSPalette = new Palette("ItemSPalette", PaletteScaleMode.Repeat, new PaletteEntry[] { paletteEntry });
this.chartItemSSaleSumAmt.PaletteRepository.Add("ItemSPalette", ItemSPalette);
this.chartItemSSaleSumAmt.PaletteName = "ItemSPalette";
//获取小分类的统计信息
NameValueList para = new NameValueList();
string brandCode = FormularContext.Current.Data["UserBrandCode"] as string;
string shopCode = FormularContext.Current.Data["UserShopCode"] as string;
string userID = Formular.BaaS.FormularContext.Current.User.UserName;
para.Add("BrandCode", brandCode);
para.Add("ShopCode", shopCode);
para.Add("UserType", _userType);
para.Add("UserID", userID);
para.Add("Month", _selectedMonth);
para.Add("ItemMCode", selectItemMSaleSumAmtDto.ItemMCode);
List<ItemSSaleSumAmtDto> listSItem = null;
try
{
listSItem = _commonService.SearchMainItemSSaleSumAmtDtoList(para);
}
catch (Exception ex)
{
MessageHelper.Error(this, ex);
}
chartItemSSaleSumAmt.DataSource = listSItem;
}