public static void SetVChart() { if (VZArray == null || VArray == null) { MessageBox.Show("array is null"); return; } VTable = vtkTable.New(); VTable.AddColumn(VZArray); VTable.AddColumn(VArray); VChart = vtkChartXY.New(); var line = VChart.AddPlot(0); line.SetInput(VTable, 0, 1); VView = vtkContextView.New(); VView.GetScene().AddItem(VChart); RenWinControlVertical.RenderWindow.GetRenderers().GetFirstRenderer().RemoveAllViewProps(); VView.SetRenderWindow(RenWinControlVertical.RenderWindow); RenWinControlVertical.RenderWindow.Render(); }
public static void SetHChart() { if (HArray == null || HZArray == null) { MessageBox.Show("array is null"); return; } HTable = vtkTable.New(); HTable.AddColumn(HArray); HTable.AddColumn(HZArray); HChart = vtkChartXY.New(); var line = HChart.AddPlot(0); line.SetInput(HTable, 0, 1); HView = vtkContextView.New(); HView.GetScene().AddItem(HChart); RenWinControlHorizontal.RenderWindow.GetRenderers().GetFirstRenderer().RemoveAllViewProps(); HView.SetRenderWindow(RenWinControlHorizontal.RenderWindow); RenWinControlHorizontal.RenderWindow.Render(); }
public static void LinePlotTest() { vtkTable table = vtkTable.New(); vtkFloatArray arrX = vtkFloatArray.New(); table.AddColumn(arrX); vtkFloatArray arrSine = vtkFloatArray.New(); table.AddColumn(arrSine); int numPoints = 100; table.SetNumberOfRows(100); for (int i = 0; i < numPoints; i++) { arrSine.SetValue(i, (float)Math.Cos(i)); } table.Update(); vtkContextView view = vtkContextView.New(); view.GetRenderer().SetBackground(0, 0, 0); vtkChartXY chart = vtkChartXY.New(); view.GetScene().AddItem(chart); chart.AddPlot(0).SetInput(table); view.GetInteractor().Initialize(); view.GetInteractor().Start(); }
protected virtual void PlotInternal(CodeContext context, ndarray array, IList <string> names = null, Type type = null) { VtkChartXyModel model = this.Model as VtkChartXyModel; if (model == null) { return; } int ndim = array.ndim; if (type == null || !type.IsSubclassOf(typeof(VtkPlotPointsModel))) { type = typeof(VtkPlotPointsModel); } ConstructorInfo cInfo = type.GetConstructor(new Type[] { }); if (cInfo == null) { return; } switch (ndim) { case 1: { // Treat array as y-values VtkPlotPointsModel plot = cInfo.Invoke(new object[] {}) as VtkPlotPointsModel; if (plot == null) { return; } int len = (int)array.Dims[0]; ndarray linspace = Math.General.LinSpace(context, len, 0.0, 1.0); vtkDoubleArray arrX = NumpyVtk.NumpyToVtk(linspace) as vtkDoubleArray; vtkDoubleArray arrY = NumpyVtk.NumpyToVtk(array) as vtkDoubleArray; if (arrX == null || arrY == null) { this.Log.Error("Plot(): Could not cast ndarrays to vtkDoubleArrays"); return; } numPlots++; arrX.SetName(DEFAULT_X_ARRAY + '_' + numPlots); if (names != null && !String.IsNullOrEmpty(names[0])) { arrY.SetName(names[0]); } else { numPlots++; arrY.SetName(DEFAULT_Y_ARRAY + '_' + numPlots); } this.SetXArray(arrX, plot); this.SetYArray(arrY, plot); model.Plots.Add(plot); } break; case 2: { // Treat 1st array as x-values, rest as y-values // No need to check for dimensional conformity, ndarray won't allow asymmetric arrays to be created ndarray arrayX = array[0] as ndarray; if (arrayX == null) { this.Log.Error("Plot(): Could not cast X-data to ndarray"); return; } vtkDoubleArray arrX = NumpyVtk.NumpyToVtk(arrayX) as vtkDoubleArray; if (arrX == null) { this.Log.Error("Plot(): Could not cast X-ndarray to vtkDoubleArray"); return; } if (names != null && !String.IsNullOrEmpty(names[0])) { arrX.SetName(names[0]); } else { numPlots++; arrX.SetName(DEFAULT_X_ARRAY + '_' + numPlots); } int numArrays = (int)array.Dims[0]; for (int i = 1; i < numArrays; i++) { VtkPlotPointsModel plot = cInfo.Invoke(new object[] { }) as VtkPlotPointsModel; if (plot == null) { return; } ndarray arrayY = array[i] as ndarray; if (arrayY == null) { this.Log.Error("Plot(): Could not cast Y-data to ndarray"); return; } vtkDoubleArray arrY = NumpyVtk.NumpyToVtk(arrayY) as vtkDoubleArray; if (arrY == null) { this.Log.Error("Plot(): Could not cast Y-ndarray to vtkDoubleArray"); return; } if (names != null && names.Count > i && !String.IsNullOrEmpty(names[i])) { arrY.SetName(names[i]); } else { numPlots++; arrY.SetName(DEFAULT_Y_ARRAY + '_' + numPlots); } this.SetXArray(arrX, plot); this.SetYArray(arrY, plot); model.Plots.Add(plot); } } break; default: this.Log.Error("Can only plot 1D and 2D arrays"); break; } vtkChartXY chart = this._chartItem as vtkChartXY; if (chart == null) { return; } foreach (IVtkPlotItemModel plotItemModel in model.Plots) { plotItemModel.PropertyChanged += OnPlotItemChanged; vtkPlot plot = plotItemModel.PlotItem as vtkPlot; if (plot != null) { chart.AddPlot(plot); } } this.RenderInternal(); }