public virtual int AddPlot(VtkPlotModel plotModel) { vtkChart chart = this._chartItem as vtkChart; vtkPlot plot = plotModel.PlotItem as vtkPlot; if (chart == null || plot == null) { return(-1); } int index = chart.AddPlot(plot); if (index == -1) { this.Log.Error("Could not add plot"); } return(index); }
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(); }