private static void AddLabels(NAxis axis, bool addTicks, NLabelInfo[] labels) { // First add custom labels NLinearScaleConfigurator scale = axis.ScaleConfigurator as NLinearScaleConfigurator; for (int i = 0; i < labels.Length; i++) { NLabelInfo labelInfo = labels[i]; NCustomValueLabel label = new NCustomValueLabel(); label.Value = labelInfo.Value; label.Text = labelInfo.Text; label.Style.Offset = new NLength(10); label.Style.TextStyle.FillStyle = new NColorFillStyle(labelInfo.ForeColor); scale.CustomLabels.Add(label); if (addTicks) { scale.CustomMajorTicks.Add(labelInfo.Value - 0.5); } } axis.UpdateScale(); // then add background coloring NScaleLevel level = (NScaleLevel)axis.Scale.Levels[axis.Scale.Levels.Count - 1]; level.TopPadding = new NLength(-1); NScaleLevel rulerLevel = (NScaleLevel)axis.Scale.Levels[0]; rulerLevel.TopPadding = new NLength(0); rulerLevel.BottomPadding = new NLength(0); NCustomScaleDecorator scaleDecorator = new NCustomScaleDecorator(); for (int i = 0; i < labels.Length; i++) { NLabelInfo labelInfo = labels[i]; if (labelInfo.BackColor != Color.Transparent) { scaleDecorator.Decorations.Add(new NScaleRange(0, new NScaleRangeDecorationAnchor(new NRange1DD(labelInfo.Value - 0.5, labelInfo.Value + 0.5)), new NColorFillStyle(labelInfo.BackColor), new NLength(0), new NLength(20), new NLength(20))); } } level.Decorators.Add(scaleDecorator); }
/// <summary> /// Displays a custom label at the secondary Y axis /// </summary> /// <param name="chart"></param> /// <param name="value"></param> /// <param name="label"></param> void SetValueLabel(NChart chart, double value, string label, bool showValue) { NScaleConfigurator scaleY2 = chart.Axis(StandardAxis.SecondaryY).ScaleConfigurator; string text = showValue ? string.Format("{0} = {1:0.###}", label, value) : label; NCustomValueLabel cl = new NCustomValueLabel(value, text); cl.Style.TextStyle.FontStyle = new NFontStyle("Arial", 8); cl.Style.ContentAlignment = ContentAlignment.TopCenter; scaleY2.CustomLabels.Add(cl); }
NDocument CreateDocument(NSize chartSize, NCustomToolsData.NData data, int populationDataId, int dataPointId) { NDocument document = new NDocument(); document.RootPanel.Charts.Clear(); // set a chart title string sex; string total; if (populationDataId == data.TotalFemaleData.Id) { sex = "Female"; total = string.Format("{0:0,#} +/-{1:0,#}", data.TotalFemaleData.Rows[dataPointId].Value, data.TotalFemaleData.Rows[dataPointId].Error); } else { sex = "Male"; total = string.Format("{0:0,#} +/-{1:0,#}", data.TotalMaleData.Rows[dataPointId].Value, data.TotalMaleData.Rows[dataPointId].Error); } NLabel header = document.RootPanel.Labels.AddHeader(string.Format("{0}, {1}, Population Data per Race<br/><font size='9pt'>Total of All Races: {2}</font>", sex, data.AgeRanges[dataPointId].Title, total)); header.TextStyle.TextFormat = TextFormat.XML; header.TextStyle.FontStyle = new NFontStyle("Times New Roman", 13, FontStyle.Italic); header.TextStyle.StringFormatStyle.HorzAlign = HorzAlign.Left; header.ContentAlignment = ContentAlignment.BottomRight; header.Location = new NPointL( new NLength(3, NRelativeUnit.ParentPercentage), new NLength(3, NRelativeUnit.ParentPercentage)); // add the chart NCartesianChart chart = new NCartesianChart(); document.RootPanel.Charts.Add(chart); chart.SetPredefinedChartStyle(PredefinedChartStyle.HorizontalLeft); chart.Margins = new NMarginsL(9, 40, 9, 9); chart.Location = new NPointL( new NLength(0, NRelativeUnit.ParentPercentage), new NLength(0, NRelativeUnit.ParentPercentage)); chart.Size = new NSizeL( new NLength(100, NRelativeUnit.ParentPercentage), new NLength(100, NRelativeUnit.ParentPercentage)); chart.Width = chartSize.Width + 180; chart.Height = chartSize.Height; NLinearScaleConfigurator scaleY = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator; scaleY.LabelValueFormatter = new NNumericValueFormatter("0,,.#M"); NOrdinalScaleConfigurator scaleX = new NOrdinalScaleConfigurator(); chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator = scaleX; scaleX.AutoLabels = false; scaleX.MajorTickMode = MajorTickMode.CustomTicks; scaleX.CustomLabelFitModes = new LabelFitMode[] { LabelFitMode.AutoScale }; scaleX.CustomLabelsLevelOffset = new NLength(4); NBarSeries barSeries = chart.Series.Add(SeriesType.Bar) as NBarSeries; barSeries.DataLabelStyle.Visible = false; int length = data.Races.Count; for (int i = 0; i < length; i++) { NCustomToolsData.NRace race = data.Races[i]; double value; if (populationDataId == race.MaleData.Id) { value = race.MaleData.Rows[dataPointId].Value; } else { value = race.FemaleData.Rows[dataPointId].Value; } barSeries.Values.Add(value); NCustomValueLabel vl = new NCustomValueLabel(i, race.Title); vl.Style.ContentAlignment = ContentAlignment.MiddleRight; scaleX.CustomLabels.Add(vl); } return(document); }