WPF Visifire图表控件使用基础

https://www.cnblogs.com/wyuan/archive/2012/07/22/WPF.html

引言:

  由于项目中需要使用Visifire所以自己就写了一些demo,大家一起共享!

基础Visifire图表的展示

1.Visifire的创建需要引用的DLL包【WPFToolkit.dll;WPFVisifire.Charts;WPFVisifire.Gauges(这个以后会用到)】

2.我们开始创建简单的Visifire图表

第一步:前台代码

<Window x:Class="Wpf_Tray.VisifireWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vc="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts"
        Title="VisifireWindow" Height="378" Width="536" WindowStartupLocation="CenterScreen">
    <Grid Name="LayoutRoot">
      <vc:Chart Name="chart" DockPanel.Dock="Left" Margin="0,40,0,2" />
      <Button Content="showChartData" Height="23" HorizontalAlignment="Left" Margin="24,8,0,0" Name="btn_showChartData" VerticalAlignment="Top" Width="97" Click="btn_showChartData_Click" />
       <Button Content="ExportToPng" Height="23" HorizontalAlignment="Left" Margin="134,8,0,0" Name="btn_ExportToPng" VerticalAlignment="Top" Width="88" Click="btn_ExportToPng_Click" />
        <Button Content="ExportChart" Height="23" HorizontalAlignment="Left" Margin="233,8,0,0" Name="btn_ExportChart" Click="btn_ExportChart_Click" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

写完后的一个显示效果,如图;
http://pic002.cnblogs.com/images/2012/368829/2012072216004293.png

第二步:

实现后台绑定数据,上代码:

/// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            DataSet ds = DBSQLHelper.Search("select * from hospitalorg", null, CommandType.Text);
            BindChart(ds.Tables[0]);
        }
      public VisifireWindow()
        {
            InitializeComponent(); 
            BindData();
        }

http://pic002.cnblogs.com/images/2012/368829/2012072216483217.png

这是绑定一列的!

第三步:

如图,有三个按钮,‘showChartData’,‘ExportToPng’

1.showChartData,主要是多列数据绑定,实现效果如图:
http://pic002.cnblogs.com/images/2012/368829/2012072216592860.png

上代码:

#region 可以显示多列,绑定界面Chart

        private void BindMoreColumnChart(DataTable dtChart)
        {
            this.chart.Series.Clear();

            this.chart.AnimationEnabled = true;

            this.chart.View3D = true;

            DataSeries dataSeries = new DataSeries();

            dataSeries.RenderAs = RenderAs.Bar;

            dataSeries.LabelEnabled = true;

            dataSeries.LegendText = "最小值";//图例显示的信息

            dataSeries.LabelText = "#AxisXLabel, #YValue";

            DataPoint datapoint;

            for (int i = 0; i < dtChart.Rows.Count; i++)
            {

                datapoint = new DataPoint();

                datapoint.AxisXLabel = dtChart.Rows[i]["job_id"].ToString();

                datapoint.YValue = Convert.ToDouble(dtChart.Rows[i]["min_lvl"].ToString());

                dataSeries.DataPoints.Add(datapoint);

            }

            this.chart.Series.Add(dataSeries);




            DataSeries dataSeries1 = new DataSeries();

            dataSeries1.RenderAs = RenderAs.Bar;

            dataSeries1.LabelEnabled = true;

            DataPoint datapoint1;

            for (int i = 0; i < dtChart.Rows.Count; i++)
            {

                datapoint1 = new DataPoint();

                datapoint1.AxisXLabel = dtChart.Rows[i]["job_id"].ToString();

                datapoint1.YValue = Convert.ToDouble(dtChart.Rows[i]["max_lvl"].ToString());

                dataSeries1.DataPoints.Add(datapoint1);
            }

            dataSeries1.LegendText = "最大值";

            dataSeries1.LabelText = "#AxisXLabel, #YValue";

            this.chart.Series.Add(dataSeries1);

            this.chart.ShadowEnabled = true;
        }
        #endregion

2.ExportTopng,是将visifire当前实现的图表导成png,上代码:

#region ExportToPng
        /// <summary>
        /// ExportToPng
        /// </summary>
        /// <param name="path"></param>
        /// <param name="surface"></param>
        public void ExportToPng(Uri path,Visifire.Charts.Chart surface)
        {
            if (path == null) return;
            //Save current canvas transform 保存当前画布变换
            Transform transform = surface.LayoutTransform;
            //reset current transform (in case it is scaled or rotated) 重设当前画布(如果缩放或旋转)
            surface.LayoutTransform = null;
            //Create a render bitmap and push the surface to it 创建一个渲染位图和表面
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
                (int)surface.Width,
                (int)surface.Height,
                96d, 96d,
                PixelFormats.Pbgra32);
            renderBitmap.Render(surface);
            // Create a file stream for saving image
            using (FileStream outStream = new FileStream(path.LocalPath,FileMode.Create))
            {
                //Use png encoder for our data
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                // push the rendered bitmap to it
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                // save the data to the stream
                encoder.Save(outStream);
            }
            // Restore previously saved layout 恢复以前保存布局
            surface.LayoutTransform = transform;
        }
        #endregion

#region 将Visifire图表保存为图片  http://www.visifire.com/blog/page/15/

        private void btn_ExportToPng_Click(object sender, RoutedEventArgs e)
        {
            ExportToPng(new Uri("D:/Visifire.png"), this.chart);
        }
#endregion

【这只是基础,visifire官方网站的文档进行学习,http//www.visifire.com/】

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是对WPFVisifire3.6.8源代码进行的简单扩展,本源代码中包含了各种实例以及一个完善的程序:内存监视器 1>修改了线Legend样式,并放大了,源系统Legend太小 2、设置了圆环内径可设定,原代码内径为外径的1/2 3、调整了柱状图的间隔系数由0.1改为0.2,要不然,当柱状图比较多时,缝隙太小,给人的视觉感觉就是夹了一个白线 4、添加了滚轮缩放和鼠标拖放操作(见里面的程序:内存监视器) 5、添加了对DataPoints的绑定支持,这是因为使用DataSource绑定时,在动态曲线的情况下,内存无法释放,而使用DataPoints 绑定则可以及时释放内存,这点例子里面也使用了几种绑定的动态曲线例子,并可通过另一个例子:内存监视器来检测不同的 数据绑定的内存释放情况。值得说明的是即使最新的WPFVisifire5.1.7版本,在使用DataSource绑定时,内存也不能及时释放。 还有一些其他的小改进 感觉修改WPFVisifire的代码相当困难,很多在不断的尝试下进行的,这点和微软的WPF Charts相比,确实不一样,微软的WPF Charts 是完全基于WPF编程思想进行的,注重其神而忽略其形,很多样式模板都是可以在外部修改的,相应的有关外形定义的属性则很少, 而这点上,WPFVisifire则相对比价传统,几乎没定义多少模板,但提供了超多的外形属性,连字体大小、颜色等各种属性都能设定。 在数据性能上,微软的WPF Charts比较差,加载4000个数据就慢得不行,WPFVisifire3.6.8一万多个都没问题,更高的没测试, 而最新的那个5.1.7版本在我的电脑上加载10W个数据在0.7秒左右,所以要想高性能,商业上还是得花那么一点钱买正版。 微软的Chart扩展:http://download.csdn.net/detail/maiker/9646423
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值