Jump to content
Software FX Community

Multiple issues with standard and custom legends


Ozzard
 Share

Recommended Posts

Chart FX 7 for WinForms with statistics extensions - so I think I'm in the right forum, but please redirect me if required :-).

I'm trying to create a histogram chart (with multiple series, possibly each with its own histogram) by putting data straight into it, rather than data binding.  Code is Listing 1 at the end.

Problem 1: The legend is initially displayed correctly, but the series names revert to "Series 1", "Series 2" etc as soon as I do anything using the Chart FX toolbar.  If I turn on bin counts via code, the labels stay intact.  If I click the toolbar button to show counts, the labels are changed (and there's sometimes a display glitch as well).

Problem 2: To bypass this, I've tried creating custom legend items and setting the Visible item attribute for the built-in series to false (Listing 2).  The custom items are (correctly) displayed, and the standard items are (correctly) hidden.  However, the border around the legend is (incorrectly) sized for all the legend items, including the invisible ones.

Problem 3: To get round that little lot, I've tried telling the legend to autosize, then resizing it based on the automatically-calculated size.  Except that the size isn't calculated until display time, so the height I read is always 0px.

Ideally, I would like:
1) A way of preventing the series names reverting.
2) If there's no way of doing 1, a way of getting the border of the legend box to surround only the visible legend items.
3) If there's no way of doing 1 or 2, a way of setting the height that doesn't make assumptions about Chart FX's internal constants such as the height of a legend box item.

All ideas welcome.

- Peter

-- Listing 1 --
  private void SetChartFromOptions()
  {
  switch (options.ChartType)
  {
  case ChartExplorerChartType.Histogram:
  ChartFX.WinForms.Statistical.Histogram gallery = statistics1.Gallery.Histogram;
  chart.GalleryAttributes = gallery;
  gallery.ShowNormal = chkShowNormal.Checked;
  chart.AxisY.Title.Text = "Count";
  statistics1.Studies.Add(StudyGroup.CentralTendencyMedian);
  statistics1.Studies.Add(StudyGroup.CentralTendencyMean);
  statistics1.LegendBox.Visible = true;
  break;
  default:
  throw new ArgumentOutOfRangeException("options.ChartType", options.ChartType, "Unknown chart type");
  }

  chart.Series.Clear();
  chart.Data.Clear();

  // Find how many series
  int seriesNumber = 0;
  foreach (Variable v in options.Parameters["data"].AsDataFrame.Variables)
  if (v.IsDoubleVariable)
  seriesNumber++;
  chart.Data.Series = seriesNumber;
  chart.Data.CommitChanges(); // TODO: Why is this necessary?

  // Add the series
  seriesNumber = 0;
  foreach (Variable v in options.Parameters["data"].AsDataFrame.Variables)
  {
  if (v.IsDoubleVariable)
  {
  DoubleVariable dv = v.AsDoubleVariable;
  if (dv.Data.Length > chart.Data.Points)
  chart.Data.Points = dv.Data.Length;
  for (int i = 0; i < dv.Data.Length; i++)
  chart.Data[seriesNumber, i] = dv.Data;
  chart.Series[seriesNumber].Text = dv.Title;
  seriesNumber++;
  }
  }
  chart.LegendBox.Visible = seriesNumber > 1;
  }
-- End of Listing 1 --

-- Listing 2 --
  private void SetChartFromOptions()
  {
  switch (options.ChartType)
  {
  case ChartExplorerChartType.Histogram:
  ChartFX.WinForms.Statistical.Histogram gallery = statistics1.Gallery.Histogram;
  chart.GalleryAttributes = gallery;
  gallery.ShowNormal = chkShowNormal.Checked;
  chart.AxisY.Title.Text = "Count";
  statistics1.Studies.Add(StudyGroup.CentralTendencyMedian);
  statistics1.Studies.Add(StudyGroup.CentralTendencyMean);
  statistics1.LegendBox.Visible = true;
  break;
  default:
  throw new ArgumentOutOfRangeException("options.ChartType", options.ChartType, "Unknown chart type");
  }

  chart.Series.Clear();
  chart.Data.Clear();

  // Find how many series
  int seriesNumber = 0;
  foreach (Variable v in options.Parameters["data"].AsDataFrame.Variables)
  if (v.IsDoubleVariable)
  seriesNumber++;
  chart.Data.Series = seriesNumber;
  chart.Data.CommitChanges(); // TODO: Why is this necessary?

  // Add the series
  seriesNumber = 0;
  foreach (Variable v in options.Parameters["data"].AsDataFrame.Variables)
  {
  if (v.IsDoubleVariable)
  {
  DoubleVariable dv = v.AsDoubleVariable;
  if (dv.Data.Length > chart.Data.Points)
  chart.Data.Points = dv.Data.Length;
  for (int i = 0; i < dv.Data.Length; i++)
  chart.Data[seriesNumber, i] = dv.Data;
  chart.LegendBox.ItemAttributes[chart.Series, seriesNumber].Visible = false;
  chart.Series[seriesNumber].MarkerShape = MarkerShape.Rect;

  // Set up custom legend item
  CustomLegendItem legendItemA = new CustomLegendItem();
  legendItemA.Text = dv.Title;
  legendItemA.MarkerShape = chart.Series[seriesNumber].MarkerShape;
  legendItemA.Color = chart.Series[seriesNumber].Color;
  chart.LegendBox.CustomItems.Add(legendItemA);

  seriesNumber++;
  }
  }
  chart.LegendBox.Visible = seriesNumber > 1;
  }
-- End of Listing 2 --

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...