C#-WPF-LiveChart大数据时图标绘制(曲线图)并支持图片保存

2022/8/17 1:56:07

本文主要是介绍C#-WPF-LiveChart大数据时图标绘制(曲线图)并支持图片保存,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
<Button
    Name="SaveBtn"
    Grid.Row="0"
    Width="100"
    Height="32"
    HorizontalAlignment="Center"
    VerticalAlignment="Top"
    Command="{Binding SavePicture}"
    CommandParameter="{Binding ElementName=Pic}"
    Content="保存图片" />

<ContentControl Grid.Row="1">
    <lvc:CartesianChart
        Name="Pic"
        Width="auto"
        Height="auto"
        Margin="5"
              <!--鼠标悬停展示点位,因为数据量较大所以取消该功能,下面的动画效果也取消-->
        DataTooltip="{x:Null}"
        DisableAnimations="True"
        Hoverable="False"
        Series="{Binding LineSeriesCollection}"
        Zoom="None">
        <lvc:CartesianChart.AxisY>
            <lvc:Axis
                Title="光亮度/lux"
                FontSize="12"
                MaxValue="300"
                MinValue="0">
                            <!--这个设置坐标间隔-->
                <lvc:Axis.Separator>
                    <lvc:Separator IsEnabled="False" Step="5" />
                </lvc:Axis.Separator>
            </lvc:Axis>
        </lvc:CartesianChart.AxisY>
        <lvc:CartesianChart.AxisX>
            <lvc:Axis FontSize="12" Labels="{Binding LineXLabels}" />
        </lvc:CartesianChart.AxisX>
    </lvc:CartesianChart>
</ContentControl>
private DelegateCommand<CartesianChart> _savePicture;
public DelegateCommand<CartesianChart> SavePicture => _savePicture ??= new DelegateCommand<CartesianChart>(ExecuteSavePicture);
private void ExecuteSavePicture(CartesianChart chart)
{
    //背景色,chart是前台按钮绑定参数传过来的
    chart.Background =System.Windows.Media.Brushes.White;
    //找到lvc的父级控件
    ContentControl gridData = (ContentControl)chart.Parent;
    //获取控件大小
    gridData.Measure(chart.RenderSize);
    //二次布局更新,看是否需要
    //gridData.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0), gridData.RenderSize));
    chart.Update(true, true);
    gridData.UpdateLayout();
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Title = "图片另存为";
    saveFileDialog.Filter = "jpg图片|*.JPG|png图片|*.PNG|jpeg图片|*.JPEG";
    saveFileDialog.FilterIndex = 2;
    saveFileDialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
    Nullable<bool> result = saveFileDialog.ShowDialog();//因为是WPF,相当于OK
    if (result == true)
    {
        FileStream fs = new FileStream($"{saveFileDialog.FileName}", FileMode.Create);//要保存的路径
        //对象转换成位图
        RenderTargetBitmap bmp = new RenderTargetBitmap((int)chart.ActualWidth + 10, (int)chart.ActualHeight + 10, 96, 96, PixelFormats.Pbgra32);
        bmp.Render(chart);
        //对象的集合编码转成图像流
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bmp));
        //保存到路径中
        encoder.Save(fs);
        //释放资源
        fs.Close();
        fs.Dispose();
        MessageBox.Show("保存成功");
    }
}
public void GetLineSeriesData()
{
    SelectLightByResult();//查询y轴数据
    LineSeries lineseries = new GLineSeries()//using LiveCharts.Geared;下包
    {
        Values = LightRGB.AsGearedValues().WithQuality(Quality.Low),//LightRGB是方法查到的List<T>类型的数据
        Fill = System.Windows.Media.Brushes.Transparent,
        StrokeThickness = 0.5,
        PointGeometry = null,
    };
    lineseries.DataLabels = false;//是否展示点值
    lineseries.Title = Title;
    LineSeriesCollection.Add(lineseries);
}

 



这篇关于C#-WPF-LiveChart大数据时图标绘制(曲线图)并支持图片保存的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程