我正在使用此示例,但图表显示在窗口内。我将其从网格中删除,然后将其设置为视图框的子项。
问题是 x 轴最大值和最小值太小以至于点不可见。这些点是从 1 到 500 的简单值。请参阅 x 轴仅适用于 0.045 到 -0.05。y 轴似乎是正确的,因为有 500 个点。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
Title="MainWindow" Height="350" Width="525" MouseDoubleClick="Window_MouseDoubleClick">
<Grid x:Name="Grid">
<lvc:CartesianChart x:Name="Chart" DisableAnimations="True" Zoom="Xy" Pan="Xy">
<lvc:CartesianChart.Series>
<lvc:LineSeries Values="{Binding Data}"></lvc:LineSeries>
</lvc:CartesianChart.Series>
</lvc:CartesianChart>
</Grid>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LiveCharts;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ChartValues<double> Data { get; set; } = new ChartValues<double>();
public MainWindow()
{
InitializeComponent();
DataContext = this;
Data = new ChartValues<double>(Enumerable.Range(0, 500).Select(x => (double)x));
}
private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Grid.Children.Remove(Chart);
Chart.Width = 1920;
Chart.Height = 1080;
var viewbox = new Viewbox();
viewbox.Child = Chart;
viewbox.Measure(Chart.RenderSize);
viewbox.Arrange(new Rect(new Point(0, 0), Chart.RenderSize));
Chart.Update(true, true); //force chart redraw
viewbox.UpdateLayout();
SaveToPng(Chart, "chart.png");
this.Content = viewbox;
}
private void SaveToPng(FrameworkElement visual, string fileName)
{
var encoder = new PngBitmapEncoder();
EncodeVisual(visual, fileName, encoder);
}
private static void EncodeVisual(FrameworkElement visual, string fileName, BitmapEncoder encoder)
{
var bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(visual);
var frame = BitmapFrame.Create(bitmap);
encoder.Frames.Add(frame);
using (var stream = File.Create(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "out.png"))) encoder.Save(stream);
}
}
}
我错过了什么?