CategoryChart exception when series are of different length

Open
#635 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java

Research direction

Start with the reproducible CategoryChart example and inspect org.knowm.xchart.internal.chartpart.PlotContent_Category_Bar.java at line 267, where the stack trace identifies the failure. Trace how series lengths are handled during painting and verify the example no longer throws when the series contain different numbers of samples.

Written by the indexing model from the issue text.

Description

Hello! Consider the following code:

public static void main(final String[] args) {
    final CategoryChart categoryChart = new CategoryChartBuilder().width(400).height(400).build();
    categoryChart.addSeries("one", new double[]{0.0}, new double[]{0.0});
    categoryChart.addSeries("two", new double[]{0.0, 1.0}, new double[]{0.0, 1.0});

    final JFrame frame = new JFrame();
    frame.add(new XChartPanel<>(categoryChart));
    frame.pack();
    frame.setVisible(true);
}

This produces the following stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
	at org.knowm.xchart.internal.chartpart.PlotContent_Category_Bar.doPaint(PlotContent_Category_Bar.java:267)
	at org.knowm.xchart.internal.chartpart.PlotContent_.paint(PlotContent_.java:55)
	at org.knowm.xchart.internal.chartpart.Plot_.paint(Plot_.java:37)
	at org.knowm.xchart.internal.chartpart.Plot_AxesChart.paint(Plot_AxesChart.java:35)
	at org.knowm.xchart.internal.chartpart.Plot_Category.paint(Plot_Category.java:37)
	at org.knowm.xchart.CategoryChart.paint(CategoryChart.java:309)
	at org.knowm.xchart.XChartPanel.paintComponent(XChartPanel.java:132)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)

Is this expected? I'm surprised that the plotter seems to assume that the series have an equal amount of samples.

Dominant language
Java
Stars
1.6k
Forks
400
Avg merge
3d 9h
Merged PRs (30d)
7

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from knowm/XChart

All issues in knowm/XChart

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.