2017-10-25 5 views
0

私は少し問題があります。私は画像を自分のグラフにエクスポートしたい。私はそれがbeto-rodriguez(here)によって与えられたコードで可能であることを知っていました。ライブチャートで画像を表示する

しかし、私は問題がある、私は私が画面にそれを表示することによってできたことができない。上記の画像を参照してください。右下に、画像がpngに保存されています。そして、画像の左側には、私が望むところに表示されているすべてのパラメータを含むグラフがあります。

保存した画像に同じチャート(左側)を表示することは可能ですか? 実際には、画像を自動的にキャプチャ(コピー/ペースト)するためにスレッドを使用します。

正しい画像を表示するために設定する必要があるパラメータは何ですか。事前に

Double chart

感謝。よろしくです。 よろしくお願いします。

*********************************私の後半repsonseて申し訳ありません編集

後。私はあなたが言ったことをしようとするが、私はそれを私が欲しいものにすることはできない。 私は私は何をすべきかを説明します: を私は別のクラス「GraphMaker」と呼ぶことができるクラス「MakeReport」があります。

MyGraph = new GraphMaker(); 
MyGraph = GiveAGraph(MyGraph); 

とクラス「MakeReport」をサブといくつかの値を使用してチャートを完了しますプログラム "GiveAGraph()":

GraphData.SeriesCollection.Add(new RowSeries 
    { 
     Title = Criteria, 
     Values = DataValues, 
     ScalesYAt = Index, 
     DataLabels = true 
    }); 

"GiveAGRaph()"の最後。私は、「MyGraph.GiveAnImageFromChart()」で

// Chart to Image 
MyGraph.GiveMeAnImageFromChart();  <-- to make a picture with the chart 
// Show the graph 
MyGraph.Show();      <-- for debug and test, i display it. 

は今、私は私がイメージを作成し、テスト(およびデバッグ)のために、私はそれを表示するためにそれを使用したい場合は、表示するチャートの準備ができてい"MyGraph.Show()"と同じ結果を持っていません。画像(pngとして保存)は、表示されているグラフとは異なります。

"GraphMaker" クラスに含まれるサブプログラム "GiveAnImageFromChart" がある:

public void GiveMeAnImageFromChart() 
    { 
      var viewbox = new Viewbox(); 
      myChart.Background = Brushes.White; 
      myChart.DataContext = this; 

      // myChart il faut tout mettre en paramètres 

      viewbox.Child = myChart; 
      viewbox.Measure(myChart.RenderSize); 
      viewbox.Arrange(new Rect(new Point(0, 0), myChart.RenderSize)); 

      myChart.Update(true, true); //force chart redraw 
      viewbox.UpdateLayout(); 

      SaveToPng(myChart, "chart.png"); 
      //png file was created at the root directory. 

    } 

"myChart" 変数はパブリックです。使用された "SaveToPng"プログラムは、あなたの例(here)から来ました。画像を保存するには

、私は次のような方法で試してみてください。では

public System.Drawing.Bitmap ControlToImage(Visual target, double dpiX, double dpiY) 
    { 
     if (target == null) 
     { 
      return null; 
     } 
     // render control content 
     Rect bounds = VisualTreeHelper.GetDescendantBounds(target); 
     Console.WriteLine("Bounds width = " + bounds.Width + " et bounds height = " + bounds.Height); 
     RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX/96.0), 
                 (int)(bounds.Height * dpiY/96.0), 
                 dpiX, 
                 dpiY, 
                 PixelFormats.Pbgra32); 
     DrawingVisual dv = new DrawingVisual(); 
     using (DrawingContext ctx = dv.RenderOpen()) 
     { 
      VisualBrush vb = new VisualBrush(target); 
      ctx.DrawRectangle(vb, null, new Rect(new System.Windows.Point(), bounds.Size)); 
     } 
     rtb.Render(dv); 

     //convert image format 
     MemoryStream stream = new MemoryStream(); 
     BitmapEncoder encoder = new BmpBitmapEncoder(); 
     encoder.Frames.Add(BitmapFrame.Create(rtb)); 
     encoder.Save(stream); 

     return new System.Drawing.Bitmap(stream); 
    } 

:この方法で

Bitmap ImageChart = MyGraph.ControlToImage(MyGraph, MyGraph.Width, MyGraph.Height); 

、私は "bounds.Width" と「境界ため、エラーを持っています。Height "は-8に等しい。そして最後に、私は変換するためのチャートを持っていません。

私は "ControlImage"に間違った "視覚的ターゲット"を与えて、何かが恋しいと思いますが、何が分からないのでしょうか?詳細が必要な場合は、私に尋ねてください。 ご協力いただきありがとうございます。

PS:英語にすみませます。私を訂正するのをためらってください。

答えて

0

おかげで、私は解決策を作ったホープ。私は私が正しくLivechartを使用していないことがわかった。 誰かが必要な場合は私のコードを投稿します。希望あなたを助けるため

<UserControl x:Class="MyProject.GraphMaker" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:MyProject" 
     mc:Ignorable="d" 
     d:DesignHeight="730" d:DesignWidth="1660"> 
<Grid> 
</Grid> 
</UserControl> 

そして、私のGraphMaker.xaml.csは

using System; 
using System.IO; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using LiveCharts; 
using LiveCharts.Wpf; 
using System.Collections.Generic; 
using System.ComponentModel; 


namespace MyProject 
{  
    public partial class GraphMaker : UserControl 
    { 
     // PUBLIC 
     public CartesianChart MyTestChart; 
     public SeriesCollection MySeriesCollection { get; set; } 
     public string[] Labels { get; set; } 
     public string AxisTitle { get; set; } 
     public Func<double, string> YFormatter { get; set; } 
     public Axis Axis1, Axis2, Axis3, Axis4, Axis5, Axis6, Axis7, Axis8, Axis9, Axis10, AxisXChart; 


     public GraphMaker() 
     { 
      InitializeComponent(); 
      MySeriesCollection = new SeriesCollection(); 

      MyTestChart = new CartesianChart 
      { 
       DisableAnimations = true, 
       Width = 1600, 
       Height = 700, 
       Series = MySeriesCollection 
      }; 

      MyTestChart.LegendLocation = LegendLocation.Right; 

      // *** Axis 1 *** 
      Axis1 = new Axis(); 
      Axis1.Foreground = Brushes.DodgerBlue; 
      Axis1.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis1.LabelFormatter = YFormatter; 
      MyTestChart.AxisY.Add(Axis1); 

      // *** Axis 2 *** 
      Axis2 = new Axis(); 
      Axis2.Foreground = Brushes.IndianRed; 
      Axis2.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis2.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis2); 

      // *** Axis 3 *** 
      Axis3 = new Axis(); 
      Axis3.Foreground = Brushes.Gold; 
      Axis3.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis3.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis3); 

      // *** Axis 4 *** 
      Axis4 = new Axis(); 
      Axis4.Foreground = Brushes.Gray; 
      Axis4.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis4.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis4); 

      // *** Axis 5 *** 
      Axis5 = new Axis(); 
      Axis5.Foreground = Brushes.DeepSkyBlue; 
      Axis5.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis5.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis5); 

      // *** Axis 6 *** 
      Axis6 = new Axis(); 
      Axis6.Foreground = Brushes.HotPink; 
      Axis6.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis6.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis6); 

      // *** Axis 7 *** 
      Axis7 = new Axis(); 
      Axis7.Foreground = Brushes.Orange; 
      Axis7.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis7.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis7); 

      // *** Axis 8 *** 
      Axis8 = new Axis(); 
      Axis8.Foreground = Brushes.RoyalBlue; 
      Axis8.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis8.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis8); 

      // *** Axis 9 *** 
      Axis9 = new Axis(); 
      Axis9.Foreground = Brushes.Black; 
      Axis9.Position = AxisPosition.RightTop; 
      Axis9.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis9); 

      // *** Axis 10 *** 
      Axis10 = new Axis(); 
      Axis10.Foreground = Brushes.DarkTurquoise; 
      Axis10.Position = AxisPosition.RightTop; 
      YFormatter = value => value.ToString("N2"); 
      Axis10.LabelFormatter = YFormatter; 
      //MyTestChart.AxisY.Add(Axis10); 

      AxisXChart = new Axis(); 
      AxisXChart.Title = AxisTitle; 
      AxisXChart.Labels = Labels; 
     } 


     public void TakeTheChart() 
     { 
      var viewbox = new Viewbox(); 
      viewbox.Child = MyTestChart; 
      viewbox.Measure(MyTestChart.RenderSize); 
      viewbox.Arrange(new Rect(new Point(0, 0), MyTestChart.RenderSize)); 
      MyTestChart.Update(true, true); //force chart redraw 
      viewbox.UpdateLayout(); 

      SaveToPng(MyTestChart, "Chart.png"); 
      //png file was created at the root directory. 
     } 

     public 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(fileName)) encoder.Save(stream); 
     } 
    } 
} 

ファイル:

これは私のGraphMaker.xamlファイルです。

よろしくお願いいたします。さようなら。

0

あなたが指しているサンプルはメモリ内に新しいチャートインスタンスが作成されているため、別のイメージを取得しています。両方のチャート、UIに表示されているもの、メモリ内に作成している、またはこの問題で説明したように、あなただけの、UIで現在のインスタンスを印刷できます。

https://github.com/beto-rodriguez/Live-Charts/issues/243

は、BTO-RDZへ