/// <summary> /// Override in derived class to initialize the chart with data from a PXModel instance. /// The method returns the altered PXModel instance for usage in the derived class, after execution of the base class. /// </summary> /// <param name="px">The PXModel instance containing the data.</param> protected virtual PXModel initializePxData(PXModel px) { List <Variable> eliminated = new List <Variable>(); if (SpecificVariableOnX != null && px.Meta.Variables.Any(v => v.Name.Equals(SpecificVariableOnX))) { px = px.PivotSpecificVariableToAloneInHead(SpecificVariableOnX); } else if (AutoMoveMostValuesVariableToX && !px.HasMostValuesVariableLastInHead()) { px = px.PivotSpecificVariableToAloneInHead(px.MostValuesVariable().Name); } if (Series.Count > 0) { throw new PxChartExceptions.AddingDataToPxChartException("Can't add data from PXModel to a PxChart that already contains data."); } eliminated = new List <Variable>(); foreach (Variable v in px.Meta.Variables.Where(x => x.Values.Count == 1 && (EliminateAllSingleValueVariables || x.Elimination))) { if (px.Meta.Variables.Count > 1) { eliminated.Add(v); px = px.Eliminate(v.Code); } } if (ChartTypes.Contains(SeriesChartType.Pie) && px.Meta.Variables.Count > 1) { throw new PxChartExceptions.PxChartException("Can't create PxChart with more than one variable as Pie Chart."); } DataBindCrossTable( px.AsDataTableCompressed(null, AsPopulationPyramid ? MaleValueNameForPopulationPyramid : null).AsEnumerable(), "Series", "Label", "DATA", "Codes=CODES,Text=TEXT" ); if (MaximumNumberOfSeriesAllowed != null && Series.Count > MaximumNumberOfSeriesAllowed) { OnMaximumNumberOfSeriesExceeded?.Invoke(Series.Count, (int)MaximumNumberOfSeriesAllowed); } if (OverrideTitle != null) { Title = OverrideTitle; } else { Title = px.Meta.Title + ". " + (eliminated.Count > 0 ? String.Join(", ", eliminated.Select(x => x.Values.First().Value).ToArray()) : ""); if (!px.Meta.ContentInfo.Units.Equals(String.Empty) && showUnit) { Title += ". " + px.Meta.ContentInfo.Units; } } if (AdjustAxes) { if (this.HasAnySeriesOfTypeGroup(PxChartExtensions.ChartTypesRotated)) { foreach (var p in Series.First().Points.Where(p => p.AxisLabel.Length > 30)) { p.AxisLabel = p.AxisLabel.InsertBreaks(30); } } } return(px); }
/// <summary> /// Returns whether the last of the variables in the table head is also the variable that has the most values. /// </summary> /// <param name="px"></param> /// <returns></returns> public static bool HasMostValuesVariableLastInHead(this PXModel px) { return (px.Meta.Heading.Count > 0 && px.Meta.Heading.Last() == px.MostValuesVariable()); }
void updateImage() { #if !DEBUG try { #endif Text = (!fileName.Equals(String.Empty) ? fileName + " - " : "") + "Test af PxChart BETA"; chartSizeLabel.Text = "Str.: " + pxChart.Width + ", " + pxChart.Height; if (button2.Text == "Dst&Chart") { dstChart.AddPxData( px, c => { //c.PieLabelStyle = "Outside"; ((DstChart)c).PXMeta = px != null ? px.Meta : null; //c.AllowDecimalInterval = false; //c.MaleValueNameForPopulationPyramid = chartType.SelectedIndex == 34 ? "mænd" : null; c.MaleValueNameForPopulationPyramid = chartType.SelectedIndex == 34 ? px.Meta.Variables.First(x => x.Values.Count == 2).Values.First().Value : null; c.ChartTypes = chartTypes.Concat(new SeriesChartType[] { (SeriesChartType)chartType.SelectedIndex }).ToArray(); c.AutoMoveMostValuesVariableToX = autoPivot.Checked; c.OverrideTitle = showTitle.Checked ? null : ""; c.HideLegend = !showLegend.Checked; //((DstChart)c).ColorCollectionsForChart = // new List<Color[]>() // { // new Color[] { Color.Bisque, Color.Beige, Color.Blue, Color.Brown } // }; if (showCopyright.Checked) { c.InfoText = "© Danmarks Statistik"; //if (px != null && px.Meta.Matrix != "") // c.InfoText += ", kilde: http://www.statistikbanken.dk/" + px.Meta.Matrix.ToLower(); } else { c.InfoText = ""; } //c.AutoMoveMostValuesVariableToX = false; //c.HideLegend = false; //c.LegendHeightInPixels = 40; c.AdjustAxes = adjust.Checked; //c.SortDataPoints = sort.Checked ? PxChart.SortType.Descending : PxChart.SortType.None; c.SortDataPoints = (PxChart.SortType)Enum.Parse(typeof(PxChart.SortType), (listBox1.SelectedItem ?? "None").ToString()); //c.DecimalPlaces = 5; //c.AllowDecimalInterval = false; try { c.Font.Family = new FontFamily("Arial"); c.Font.SizeRegular = 13; //c.Font.SizeSmall = 23; } catch { } } ); //if (adjust.Checked) // dstChart.adjustAxes(); //if (sort.Checked) // dstChart.Sort(); //dstChart.ChartAreas["Main"].AxisX.LabelStyle.Angle = 45; //dstChart.ChartAreas["Main"].AxisX.IsLabelAutoFit = false; //dstChart.ChartAreas.First().AxisY.LabelStyle.Format = "{0:#,##0.##}"; //dstChart.AntiAliasing = AntiAliasingStyles.All; //dstChart.Units = "Mio. kr."; //dstChart.ChartAreas.First().AxisY.LabelStyle.Format = "N"; //dstChart.ChartAreas.First().AxisX.LabelStyle.Format = "N"; //dstChart.ChartAreas.First().AxisX.IsLabelAutoFit = true; } if (button2.Text == "Px&Chart") { pxChart.AddPxData( px, c => { //c.AllowDecimalInterval = false; //c.MaleValueNameForPopulationPyramid = chartType.SelectedIndex == 34 ? "mænd" : null; c.MaleValueNameForPopulationPyramid = chartType.SelectedIndex == 34 ? px.Meta.Variables.First(x => x.Values.Count == 2).Values.First().Value : null; c.ChartTypes = chartTypes.Concat(new SeriesChartType[] { (SeriesChartType)chartType.SelectedIndex }).ToArray(); c.AutoMoveMostValuesVariableToX = autoPivot.Checked; c.InfoText = showCopyright.Checked ? "© Danmarks Statistik" : ""; c.AdjustAxes = adjust.Checked; //c.SortDataPoints = sort.Checked ? PxChart.SortType.Descending : PxChart.SortType.None; c.SortDataPoints = (PxChart.SortType)Enum.Parse(typeof(PxChart.SortType), (listBox1.SelectedItem ?? "None").ToString()); } ); //if (adjust.Checked) // pxChart.adjustAxes(); //if (sort.Checked) // pxChart.Sort(); } if (button2.Text == "Ms&Chart") { msChart.Series.Clear(); if (autoPivot.Checked) { px = px.PivotSpecificVariableToAloneInHead(px.MostValuesVariable().Name); } msChart.DataBindCrossTable( px.AsDataTableCompressed().AsEnumerable(), "Series", "Label", "DATA", "" ); foreach (Series s in msChart.Series) { s.ChartType = (SeriesChartType)chartType.SelectedIndex; } } dstChart.Visible = button2.Text == "Dst&Chart"; pxChart.Visible = button2.Text == "Px&Chart"; msChart.Visible = button2.Text == "Ms&Chart"; infoLabel.Hide(); //Activate(); #if !DEBUG } catch (Exception e) { infoLabel.Text = fileName != null ? e.Message : "Åbn en px-fil for at vise en graf.\nKlik på Åbn-knappen (Alt + Å)."; infoLabel.Show(); } #endif }