找回密码
 立即注册

QQ登录

只需一步,快速开始

heyixiaoran

论坛元老

6

主题

26

帖子

9032

积分

论坛元老

积分
9032

活字格认证微信认证勋章元老葡萄

heyixiaoran
论坛元老   /  发表于:2014-7-9 16:53  /   查看:12328  /  回复:11
我想把C1的Chart当作一个自定义控件的一部分,然后DataSeries的数量是不固定的,所以想知道怎么绑定


图中1的部分是正常写死值,2的部分应该是DataSeries的绑定吧,我就找到这个,Children属性只是只读的,但是绑定后不显示值,请问是该怎么处理这个绑定



我是这么赋值的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

11 个回复

倒序浏览
Alice
社区贡献组   /  发表于:2014-7-11 09:19:00
推荐
回复 7楼heyixiaoran的帖子

你是说想绑定动态的系列,即系列按照需求自己添加,这次我理解的没错吧?
用SeriesItemSource这个方向是没错的。使用SeriesItemsSource和SeriesItemTemplate来设定绑定源和模板。
我写了一个简单的例子,希望能帮到你。
如下代码中指定X、Y轴的Series数据,你若是不需要X轴数据,删掉相关代码,然后把XYDataSeries换成DataSeries。代码我都测试过,复制到本地可以使用,代码如下:
  1. <c1:C1Chart Name="c1Chart1" >
  2.     <c1:C1Chart.Data>
  3.         <c1:ChartData SeriesItemsSource="{Binding MyReports}">
  4.             <c1:ChartData.SeriesItemTemplate>
  5.                 <DataTemplate>
  6.                     <c1:XYDataSeries ItemsSource="{Binding Data}"
  7.                                      ValueBinding="{Binding Y}"
  8.                                      XValueBinding="{Binding X}" />
  9.                 </DataTemplate>
  10.             </c1:ChartData.SeriesItemTemplate>
  11.         </c1:ChartData>
  12.     </c1:C1Chart.Data>
  13.     <c1:C1ChartLegend DockPanel.Dock="Right" />
  14. </c1:C1Chart>
复制代码
  1. /// </summary>
  2. public partial class MainWindow : Window
  3. {
  4.     public MainWindow()
  5.     {
  6.         InitializeComponent();

  7.         MyReport report = new MyReport();
  8.         ObservableCollection<MyData> data = new ObservableCollection<MyData>();
  9.         data.Add(new MyData() { X = 5, Y = 7 });
  10.         data.Add(new MyData() { X = 8, Y = 4 });
  11.         data.Add(new MyData() { X = 9, Y = 3 });
  12.         data.Add(new MyData() { X = 12, Y = 1 });
  13.         report.Data = data;

  14.         MyReport report2 = new MyReport();
  15.         ObservableCollection<MyData> data2 = new ObservableCollection<MyData>();
  16.         data2.Add(new MyData() { X = 5, Y = 5 });
  17.         data2.Add(new MyData() { X = 8, Y = 1 });
  18.         data2.Add(new MyData() { X = 9, Y = 4 });
  19.         data2.Add(new MyData() { X = 12, Y = 3 });
  20.         report2.Data = data2;

  21.         MyReports = new ObservableCollection<MyReport>();
  22.         MyReports.Add(report);
  23.         MyReports.Add(report2);

  24.         this.DataContext = this;
  25.     }

  26.     public ObservableCollection<MyReport> MyReports { get; set; }
  27. }

  28. public class MyReport
  29. {
  30.     public ObservableCollection<MyData> Data { get; set; }
  31. }

  32. public class MyData
  33. {
  34.     public double X { get; set; }
  35.     public double Y { get; set; }
  36. }
复制代码

评分

参与人数 1满意度 +5 收起 理由
heyixiaoran + 5 我很赞同

查看全部评分

请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2014-7-9 18:44:00
沙发
回复 1楼heyixiaoran的帖子

后台绑定数据源方法如下:

  1.         public MainWindow()
  2.         {
  3.             InitializeComponent();
  4.             this.c1chart1.Data.Children.Clear();

  5.             ChartData _bubbleData = new ChartData();

  6.             XYDataSeries ds1 = new XYDataSeries() { Label = &quot;s1&quot; };
  7.             ds1.ValuesSource = new double[] { 3, 4, 7, 5, 8 };
  8.             _bubbleData.Children.Add(ds1);

  9.             XYDataSeries ds2 = new XYDataSeries() { Label = &quot;s2&quot; };
  10.             ds2.ValuesSource = new double[] { 1, 2, 3, 4, 6 };
  11.             _bubbleData.Children.Add(ds2);

  12.             c1chart1.ChartType = ChartType.XYPlot;
  13.             c1chart1.Data = _bubbleData;
  14.         }
复制代码
回复 使用道具 举报
heyixiaoran
论坛元老   /  发表于:2014-7-9 19:21:00
板凳
回复 2楼iceman的帖子

hi  iceman
这个是赋值,我是要走Binding 啊,没看明白啊,我是把Chart 作为我的自定义控件的一个部分,所以把数据写成一个DependencyProperty,然后Xaml里TemplateBinding 过去的,可否详细说一下
回复 使用道具 举报
heyixiaoran
论坛元老   /  发表于:2014-7-10 11:21:00
地板
回复 2楼iceman的帖子

Hi Iceman
Data 赋值后,在Xaml里该TemplateBinding 给哪个属性,能给下Xaml 的示例嘛
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2014-7-10 15:18:00
5#
回复 4楼heyixiaoran的帖子

你好。
ItemNames可以用你的方法直接绑定。
但1楼图片中2部分SeriesItemsSource的绑定xmal有问题,不能将数据传递过去。参考代码如下:
  1. <c1:C1Chart Name="c1Chart4" ChartType="Column"  Palette="Office">
  2. <c1:C1Chart.Data>
  3. <c1:ChartData ItemsSource="{Binding ChartData}" ItemNameBinding="{Binding Product}">
  4. <c1:DataSeries Label="Value" ValueBinding="{Binding Value}" />
  5. <c1:DataSeries Label="Discount" ValueBinding="{Binding Discount}" />
  6. </c1:ChartData>
  7. </c1:C1Chart.Data>
  8. </c1:C1Chart>
复制代码


你可以根据代码写看看,做Sample还需要点时间。
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2014-7-10 16:03:00
6#
回复 3楼heyixiaoran的帖子

这个绑定需要自己写一些代码,数据源以及Xaml绑定的写法都可以参考Sample。
附件是你需要的例子。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
heyixiaoran
论坛元老   /  发表于:2014-7-10 22:28:00
7#
回复 6楼Alice的帖子

Hi Alice

可能我说的不明白,我的意思是DataSeries的个数不确定,我想把DataSeries的集合都写成绑定的形式
<c1Chart:C1Chart Name="C1Chart2D">
     <c1Chart:C1Chart.Data>
         <c1Chart:ChartData ItemNames="{TemplateBinding XAxisLables}" SeriesItemsSource="{TemplateBinding DataSeriesCollection}">
         </c1Chart:ChartData>
     </c1Chart:C1Chart.Data>

     <c1Chart:C1ChartLegend DockPanel.Dock="Right" />
</c1Chart:C1Chart>


DataSeries series1 = new DataSeries()
{
    Label = "label1",
    Values = new DoubleCollection() { 12, 23, 32, 12, 4 },
    RenderMode = RenderMode.Default
};
DataSeries series2 = new DataSeries()
{
    Label = "label2",
    Values = new DoubleCollection() { 18, 29, 37, 20, 10 },
    RenderMode = RenderMode.Default
};
ObservableCollection<DataSeries> collection = new ObservableCollection<DataSeries>();
collection.Add(series1);
collection.Add(series2);
item.DataSeriesCollection = collection;

我Binding 这个SeriesItemsSource也没看到图型,不知道怎么回事,由于我这个是在Xamarin的项目,所以只提供了这一点,我用时是把这个放在ListView 里充当ListViewItem 用的


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
heyixiaoran
论坛元老   /  发表于:2014-7-11 10:18:00
9#
回复 8楼Alice的帖子

Wow great . 这是我想要的,非常感谢您,让您一大早就这么忙,非常抱歉
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2014-7-11 13:50:00
10#
回复 9楼heyixiaoran的帖子

虽然忙碌,但能够帮你解决问题还是很开心的。
谢谢你帮忙评分。以后有问题欢迎开新帖交流。
或是自己摸索出的经验,例子都可以给我们分享。我们会有金币等奖励送出。
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部