silverlight 图表

在服务端引用Silverlight.DataSetConnector.dll

将得到的Dataset  转一下:   string res = Silverlight.DataSetConnector.Connector.ToXml(dds);

在客户端引用Silverlight.DataSet.dll

得到的结果,转成Dataset:

            ds.FromXml(e.Result);

前台:

<Grid x:Name="LayoutRoot" Background="White">
        <Canvas Margin="60,47,0,0" Height="400" Width="400" x:Name="cav" HorizontalAlignment="Left" VerticalAlignment="Top" />
        <sdk:Label Height="16" HorizontalAlignment="Left" Margin="32,24,0,0" Name="label1" VerticalAlignment="Top" Width="33" Content="图型:" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="71,20,0,0" Name="comboBox1" VerticalAlignment="Top" Width="98" SelectionChanged="comboBox1_SelectionChanged" />
        <sdk:Label Content="2D/3D:" Height="16" HorizontalAlignment="Left" Margin="232,24,0,0" Name="label2" VerticalAlignment="Top" Width="46" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="284,20,0,0" Name="comboBox2" VerticalAlignment="Top" Width="98" SelectionChanged="comboBox2_SelectionChanged" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="168,471,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>

后台:

  public partial class tubiao : UserControl
    {
        Silverlightjizhang.ServiceReference1.XWServiceClient client = new ServiceReference1.XWServiceClient();
        public tubiao()
        {
            InitializeComponent();
            client.GetDataSetCompleted += new EventHandler<ServiceReference1.GetDataSetCompletedEventArgs>(client_GetDataSetCompleted);
            client.GetDataSetAsync();
            comtu();
        }
        static Silverlight.DataSet ds = new Silverlight.DataSet();
        static Chart chart = new Chart();
        //static DataSeries s = new DataSeries();
        //static DataPoint p = new DataPoint();
        void client_GetDataSetCompleted(object sender, ServiceReference1.GetDataSetCompletedEventArgs e)
        {
            ds = new Silverlight.DataSet();
            string res = e.Result;
            ds.FromXml(res);
            chart.Width = 400;
            chart.Height = 400;
            for (int j = 1; j < ds.Tables[0].Columns.Count; j++)
            {
                DataSeries s = new DataSeries();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataPoint p = new DataPoint();
                    p.XValue = ds.Tables[0].Rows[i][0];
                    string aa = ds.Tables[0].Columns[j].ColumnName;
                    string a = ds.Tables[0].Rows[i][aa];
                    p.YValue = Convert.ToDouble(a);
                    s.DataPoints.Add(p);
                    p.MouseLeftButtonDown += new MouseButtonEventHandler(p_MouseLeftButtonDown);
                }
                s.RenderAs = RenderAs.Column;
                s.LegendText = ds.Tables[0].Columns[j].ColumnName;
                s.ShadowEnabled = false;
                chart.Series.Add(s);
            }
            cav.Children.Add(chart);
        }

        void p_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataPoint p = sender as DataPoint;
            MessageBox.Show("日期:" + p.XValue + "\n\r值:" + p.YValue);
        }

        public void comtu()
        {
            List<string> list = new List<string>();
            list.Add("--请选择--");
            list.Add("柱状图");
            list.Add("线图");
            comboBox1.ItemsSource = list;
            comboBox1.SelectedIndex = 0;

            List<string> list2 = new List<string>();
            list2.Add("2D");
            list2.Add("3D");
            comboBox2.ItemsSource = list2;
            comboBox2.SelectedIndex = 0;
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox2.SelectedValue.ToString() == "2D")
            {
                chart.View3D = false;
            }
            else if (comboBox2.SelectedValue.ToString() == "3D")
            {
                chart.View3D = true;
            }
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox1.SelectedValue.ToString() == "柱状图")
            {
                for (int i = 0; i < chart.Series.Count; i++)
                {
                    chart.Series[i].RenderAs = RenderAs.Column;
                }
                if (comboBox2.SelectedValue.ToString() == "3D")
                {
                    chart.View3D = true;
                }
            }
            else if (comboBox1.SelectedValue.ToString() == "线图")
            {
                for (int i = 0; i < chart.Series.Count; i++)
                {
                    chart.Series[i].RenderAs = RenderAs.Line;
                }
                if (comboBox2.SelectedValue.ToString() == "3D")
                {
                    chart.View3D = true;
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < chart.Series.Count; i++)
            {
                chart.Series[i].RenderAs = RenderAs.Line;
            }
        }
    }

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值