void AddNewChart(object sender, RoutedEventArgs e) { if (_excel.ActiveSheet.Selections.Count == 0) { Kit.Msg("请选择单元格区域!"); return; } CellRange range = _excel.ActiveSheet.Selections[0]; if (range.Row < 0 || range.Column < 0) { Kit.Msg("单元格区域无效!"); return; } string rangeFormula = _excel.ActiveSheet.Cells[range.Row, range.Column, range.RowCount + range.Row - 1, range.ColumnCount + range.Column - 1].ToString(_excel.ActiveSheet.Cells[0, 0]); rangeFormula = "'" + _excel.ActiveSheet.Name + "'!" + rangeFormula; SpreadChartType chartType = (SpreadChartType)Enum.Parse(typeof(SpreadChartType), (string)_cbTypes.SelectedItem, false); //SpreadChart chart = _excel.ActiveSheet.AddChart("CustomChart" + customChartIndex.ToString(), chartType, rangeFormula, 10, 10, 400, 290); SpreadChart chart = _excel.ActiveSheet.AddChart("CustomChart" + customChartIndex.ToString(), chartType, rangeFormula, 10, 0, 10, 0, 400, 290); _excel.ActiveSheet.ChartSelectionChanged += ChartSelectionChanged; chart.ChartTitle = new ChartTitle(); chart.ChartTitle.Text = chartType.ToString(); chart.IsSelected = true; customChartIndex++; }
public static bool IsChartWithMarker(SpreadChartType chartType) { if (!chartType.ToString().ToLower().Contains("marker")) { return(chartType == SpreadChartType.Scatter); } return(true); }
bool ChangeChartTypeOne(SpreadChartType fromChartType, SpreadChartType toChartType) { SpreadChartUtility.GetDataDimension(fromChartType); SpreadChartUtility.GetDataDimension(toChartType); string itemsFormula = _chart.ItemsFormula; string xValueFormula = null; _chart.ChartType = toChartType; ResetDataSeriesChartType(); List <SpreadDataSeries> list = new List <SpreadDataSeries>(); foreach (SpreadDataSeries series in _chart.DataSeries) { MemoryStream stream = new MemoryStream(); XmlWriter writer = XmlWriter.Create((Stream)stream); if (writer != null) { Serializer.SerializeObj(series, "DataSeries", writer); writer.Close(); stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin); XmlReader @this = XmlReader.Create((Stream)stream); if (@this != null) { SpreadDataSeries series2 = Dt.Cells.Data.ChangeChartTypeHelper.CreateDataSeries(toChartType); ((IXmlSerializable)series2).ReadXml(@this); if (string.IsNullOrEmpty(xValueFormula) && (series is SpreadXYDataSeries)) { xValueFormula = (series as SpreadXYDataSeries).XValueFormula; } series2.ResetChartType(); @this.Close(); list.Add(series2); } } } if (string.IsNullOrEmpty(itemsFormula) && (itemsFormula != xValueFormula)) { if (toChartType.ToString().ToLower().Contains("bar")) { _chart.AxisY.ItemsFormula = xValueFormula; _chart.AxisX.ItemsFormula = null; } else { _chart.AxisX.ItemsFormula = xValueFormula; _chart.AxisY.ItemsFormula = null; } } if (list.Count > 0) { _chart.DataSeries.Clear(); _chart.DataSeries.AddRange((IList <SpreadDataSeries>)list); } return(true); }
public static bool IsStackedChart(SpreadChartType charType) { return(charType.ToString().ToLower().Contains("stacked")); }
public static bool IsSeriesHasMarker(SpreadChartType charType) { return(charType.ToString().ToLower().Contains("marker")); }
public static bool IsRadarChart(SpreadChartType charType) { return(charType.ToString().ToLower().Contains("radar")); }
public static bool IsPieChart(SpreadChartType charType) { return(charType.ToString().ToLower().Contains("pie")); }
public static bool IsColumnChart(SpreadChartType chartType) { return(chartType.ToString().ToLower().Contains("column")); }
public static bool IsBubbleChart(SpreadChartType chartType) { return(chartType.ToString().ToLower().Contains("bubble")); }
public static bool Is100PercentChart(SpreadChartType charType) { return(charType.ToString().ToLower().Contains("100pc")); }
bool ChangeChartType(SpreadChartType fromChartType, SpreadChartType toChartType) { _chart.SuspendEvents(); bool flag = false; try { int dataDimension = Dt.Cells.Data.SpreadChartUtility.GetDataDimension(fromChartType); int num2 = Dt.Cells.Data.SpreadChartUtility.GetDataDimension(toChartType); bool flag2 = fromChartType.ToString().ToLower().Contains("bar"); bool flag3 = toChartType.ToString().ToLower().Contains("bar"); if (dataDimension != num2) { if (toChartType == SpreadChartType.StockHighLowOpenClose) { flag = ChangeToStockChart(fromChartType, toChartType); } else if (fromChartType == SpreadChartType.StockHighLowOpenClose) { flag = ChangeFromStockChart(fromChartType, toChartType); } else if (num2 != 1) { flag = ChangeChartTypeToMulti(fromChartType, toChartType); } else if (num2 == 1) { flag = ChangeChartTypeOne(fromChartType, toChartType); } } else { if (flag2 != flag3) { AxisType axisType = _chart.AxisY.AxisType; _chart.AxisY.AxisType = _chart.AxisX.AxisType; _chart.AxisX.AxisType = axisType; string itemsFormula = _chart.AxisY.ItemsFormula; _chart.AxisY.ItemsFormula = _chart.AxisX.ItemsFormula; _chart.AxisX.ItemsFormula = itemsFormula; } _chart.ChartType = toChartType; } if (flag2 != flag3) { if (flag3) { if (_chart.AxisX != null) { _chart.AxisX.ShowMajorGridlines = true; } if (_chart.AxisY != null) { _chart.AxisY.ShowMajorGridlines = false; } } if (flag2) { if (_chart.AxisY != null) { _chart.AxisY.ShowMajorGridlines = true; } if (_chart.AxisX != null) { _chart.AxisX.ShowMajorGridlines = false; } } } if (!Dt.Cells.Data.SpreadChartUtility.IsChartWithMarker(toChartType)) { return(flag); } foreach (SpreadDataSeries series in _chart.DataSeries) { if (series.MarkerType == MarkerType.None) { series.MarkerType = MarkerType.Automatic; } if ((series.MarkerType != MarkerType.None) && ((series.MarkerSize.IsEmpty || (series.MarkerSize.Width == 0.0)) || (series.MarkerSize.Height == 0.0))) { series.MarkerSize = new Size(7.0, 7.0); } } } finally { _chart.ResumeEvents(); } return(flag); }
bool ChangeFromStockChart(SpreadChartType fromChartType, SpreadChartType toChartType) { Dt.Cells.Data.SpreadChartUtility.GetDataDimension(fromChartType); int dataDimension = Dt.Cells.Data.SpreadChartUtility.GetDataDimension(toChartType); if ((_chart.DataSeries.Count <= 0) || !(_chart.DataSeries[0] is SpreadOpenHighLowCloseSeries)) { return(false); } string itemsFormula = _chart.ItemsFormula; ResetDataSeriesChartType(); _chart.ChartType = toChartType; List <SpreadDataSeries> list = new List <SpreadDataSeries>(); SpreadOpenHighLowCloseSeries series = _chart.DataSeries[0] as SpreadOpenHighLowCloseSeries; for (int i = 0; i < 4; i++) { SpreadDataSeries openSeries = null; switch (i) { case 0: openSeries = series.OpenSeries; break; case 1: openSeries = series.HighSeries; break; case 2: openSeries = series.LowSeries; break; case 3: openSeries = series.CloseSeries; break; } if (openSeries.Stroke == null) { openSeries.IsAutomaticStroke = true; } MemoryStream stream = new MemoryStream(); XmlWriter writer = XmlWriter.Create((Stream)stream); if (writer != null) { Serializer.SerializeObj(openSeries, "DataSeires", writer); writer.Close(); stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin); XmlReader @this = XmlReader.Create((Stream)stream); if (@this != null) { SpreadDataSeries series3 = Dt.Cells.Data.ChangeChartTypeHelper.CreateDataSeries(toChartType); ((IXmlSerializable)series3).ReadXml(@this); if (series3 is SpreadXYDataSeries) { SpreadXYDataSeries series4 = series3 as SpreadXYDataSeries; if (!string.IsNullOrEmpty(series.XValueFormula)) { series4.XValueFormula = series.XValueFormula; } else if (series.XValues.Count > 0) { series4.XValues = series.XValues; } } series3.ResetChartType(); @this.Close(); list.Add(series3); } } } if (((dataDimension == 1) && string.IsNullOrEmpty(itemsFormula)) && (itemsFormula != series.XValueFormula)) { if (toChartType.ToString().ToLower().Contains("bar")) { _chart.AxisY.ItemsFormula = series.XValueFormula; _chart.AxisX.ItemsFormula = null; } else { _chart.AxisX.ItemsFormula = series.XValueFormula; _chart.AxisY.ItemsFormula = null; } } if (list.Count > 0) { _chart.DataSeries.Clear(); _chart.DataSeries.AddRange((IList <SpreadDataSeries>)list); } return(true); }