private void PieChart_OnClick(object sender, RoutedEventArgs e) { try { ChartBy chartBy = this.ByRows.IsChecked == true ? ChartBy.Rows : ChartBy.Cols; Dictionary <string, double> chartData = this.GetChartDataOfSelectedCells(chartBy); bool allZeros = true; foreach (var d in chartData) { if (d.Value != 0) { allZeros = false; } } if (allZeros) { throw new Exception("Pie chart cannot be drawn if all cell values are equal to 0 or is empty"); } PieChart window = new PieChart(); window.GenerateDataSeries(chartData, chartBy); window.Show(); string chartType = chartBy == ChartBy.Rows ? "rows" : "columns"; Logger.WriteLogInfo(string.Format("Successfully drawed pie chart by {0} of selected cells", chartType)); } catch (Exception ex) { MessageBox.Show(ex.GetType().Name + ": " + ex.Message, "Error occured during drawing graph", MessageBoxButton.OK, MessageBoxImage.Error); Logger.WriteLogException(ex.GetType().Name + ": " + ex.Message); } }
public void SetDataSumValuesList(Dictionary<string, double> data, ChartBy chartBy) { // lets clear previous chart this.SumValuesCollection.Clear(); this.MainChart.Series.Clear(); // copy columns data into our observable collection foreach (var column in data) { this.SumValuesCollection.Add(new DataSumValuesCollection { new DataSumValues { Header = (chartBy == ChartBy.Cols ? "Column " : "Row ") + column.Key, SumValues = column.Value } }); } foreach (var item in this.SumValuesCollection) { BarSeries columnSeries = new BarSeries { SelectionMode = SelectionMode.Series }; BindableDataSeries bindableData = new BindableDataSeries { Title = item[0].Header, ItemsSource = item, XValueBinding = new Binding("SumValues"), YValueBinding = new Binding("Header"), }; columnSeries.DataSeries = bindableData; this.MainChart.Series.Add(columnSeries); } }
public void GenerateDataSeries(Dictionary<string,double> data, ChartBy chartBy) { var series = new DataSeries<string, double>(); foreach (var d in data) { series.Add(new DataPoint<string, double>((chartBy == ChartBy.Cols ? "Column " : "Row ") + d.Key, d.Value)); } MainChart.DataSeries = series; }
public void GenerateDataSeries(Dictionary <string, double> data, ChartBy chartBy) { var series = new DataSeries <string, double>(); foreach (var d in data) { series.Add(new DataPoint <string, double>((chartBy == ChartBy.Cols ? "Column " : "Row ") + d.Key, d.Value)); } MainChart.DataSeries = series; }
/// <summary> /// Gets sum of selected cells content /// </summary> /// <param name="chartBy"></param> /// <returns></returns> private Dictionary <string, double> GetChartDataOfSelectedCells(ChartBy chartBy) { Dictionary <string, double> drawChart = new Dictionary <string, double>(); var dataGrid = (ExtendedDataGrid)((TabItem)WorkspaceTabControl.Items[this.CurrentSheetNumber]).Content; foreach (var cellInfo in dataGrid.SelectedCells) { // this changes the cell's content not the data item behind it var gridCell = dataGrid.TryToFindGridCell(cellInfo); if (gridCell != null) { string columnHeader = gridCell.Column.Header.ToString(); int rowIndex = dataGrid.Items.IndexOf(cellInfo.Item); string coord = chartBy == ChartBy.Rows ? rowIndex.ToString() : columnHeader; var cell = this.DataTables[this.CurrentSheetNumber].GetSpreadsheetCell(rowIndex, SSColumns.Parse(columnHeader)); if (!drawChart.ContainsKey(coord)) { drawChart.Add(coord, 0); } if (cell.Content == string.Empty) { continue; } double doubleVal; if (double.TryParse(cell.Content, out doubleVal)) { drawChart[coord] += doubleVal; } else { throw new Exception("Some of selected cells contains non-numeric values!"); } } } if (drawChart.Count == 0) { throw new Exception("Please select cells to graph from!"); } return(drawChart); }
/// <summary> /// Shows bar chart in separate window /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BarChart_OnClick(object sender, RoutedEventArgs e) { try { ChartBy chartBy = this.ByRows.IsChecked == true ? ChartBy.Rows : ChartBy.Cols; Dictionary <string, double> chartData = this.GetChartDataOfSelectedCells(chartBy); BarChart window = new BarChart(); window.SetDataSumValuesList(chartData, chartBy); window.Show(); Logger.WriteLogInfo(string.Format("Successfully drawed Bar chart by {0} of selected cells", (chartBy == ChartBy.Rows ? "rows" : "columns"))); } catch (Exception ex) { MessageBox.Show(ex.GetType().Name + ": " + ex.Message, "Error occured during drawing graph", MessageBoxButton.OK, MessageBoxImage.Error); Logger.WriteLogException(ex.GetType().Name + ": " + ex.Message); } }
public void SetDataSumValuesList(Dictionary <string, double> data, ChartBy chartBy) { // lets clear previous chart this.SumValuesCollection.Clear(); this.MainChart.Series.Clear(); // copy columns data into our observable collection foreach (var column in data) { this.SumValuesCollection.Add(new DataSumValuesCollection { new DataSumValues { Header = (chartBy == ChartBy.Cols ? "Column " : "Row ") + column.Key, SumValues = column.Value } }); } foreach (var item in this.SumValuesCollection) { BarSeries columnSeries = new BarSeries { SelectionMode = SelectionMode.Series }; BindableDataSeries bindableData = new BindableDataSeries { Title = item[0].Header, ItemsSource = item, XValueBinding = new Binding("SumValues"), YValueBinding = new Binding("Header"), }; columnSeries.DataSeries = bindableData; this.MainChart.Series.Add(columnSeries); } }
/// <summary> /// Gets sum of selected cells content /// </summary> /// <param name="chartBy"></param> /// <returns></returns> private Dictionary<string, double> GetChartDataOfSelectedCells(ChartBy chartBy) { Dictionary<string, double> drawChart = new Dictionary<string, double>(); var dataGrid = (ExtendedDataGrid)((TabItem)WorkspaceTabControl.Items[this.CurrentSheetNumber]).Content; foreach (var cellInfo in dataGrid.SelectedCells) { // this changes the cell's content not the data item behind it var gridCell = dataGrid.TryToFindGridCell(cellInfo); if (gridCell != null) { string columnHeader = gridCell.Column.Header.ToString(); int rowIndex = dataGrid.Items.IndexOf(cellInfo.Item); string coord = chartBy == ChartBy.Rows ? rowIndex.ToString() : columnHeader; var cell = this.DataTables[this.CurrentSheetNumber].GetSpreadsheetCell(rowIndex, SSColumns.Parse(columnHeader)); if (!drawChart.ContainsKey(coord)) { drawChart.Add(coord, 0); } if (cell.Content == string.Empty) { continue; } double doubleVal; if (double.TryParse(cell.Content, out doubleVal)) { drawChart[coord] += doubleVal; } else { throw new Exception("Some of selected cells contains non-numeric values!"); } } } if (drawChart.Count == 0) { throw new Exception("Please select cells to graph from!"); } return drawChart; }
public void SetDataSumValuesList(Dictionary <string, double> data, ChartBy chartBy) { // lets clear previous chart this.SumValuesCollection.Clear(); this.MainChart.Series.Clear(); this.CustomLegend.Children.Clear(); // copy columns data into our observable collection foreach (var d in data) { this.SumValuesCollection.Add(new DataSumValuesCollection { new DataSumValues { Header = (chartBy == ChartBy.Cols ? "Column " : "Row ") + d.Key, SumValues = d.Value } }); } foreach (var item in this.SumValuesCollection) { ColumnSeries columnSeries = new ColumnSeries { SelectionMode = SelectionMode.Series }; BindableDataSeries bindableData = new BindableDataSeries { Title = item[0].Header, ItemsSource = item, XValueBinding = new Binding("Header"), YValueBinding = new Binding("SumValues") }; columnSeries.DataSeries = bindableData; this.MainChart.Series.Add(columnSeries); Rectangle rect = new Rectangle { Margin = new Thickness(5, 0, 2, 0), Height = 10, Width = 10, VerticalAlignment = VerticalAlignment.Center }; Binding fillBind = new Binding(); fillBind.ElementName = "MainChart"; fillBind.Path = new PropertyPath("Series[" + (this.MainChart.Series.Count - 1) + "].PointFill"); rect.SetBinding(Rectangle.FillProperty, fillBind); TextBlock txtblock = new TextBlock { Margin = new Thickness(0, 0, 8, 0), VerticalAlignment = VerticalAlignment.Center }; Binding textBind = new Binding(); textBind.ElementName = "MainChart"; textBind.Path = new PropertyPath("Series[" + (this.MainChart.Series.Count - 1) + "].DataSeries.Title"); txtblock.SetBinding(TextBlock.TextProperty, textBind); this.CustomLegend.Children.Add(rect); this.CustomLegend.Children.Add(txtblock); } }