2

I need to change this code in such a way that X Axis contains time stamps in the format "H:M", e.g. 10:00.

private static XYDataset createCategoryDataset(Map<Integer,List<Task>> staffLevels) 
        {
              String series1 = "Task demand";

              DefaultXYDataset dataset = new DefaultXYDataset();

              double[][] data = new double[2][staffLevels.size()];
              int min_per_hour = 60;

              for (int i=0; i<staffLevels.size(); i++)
              {
                  int seconds = i*Parameters.MIN_TIME_UNIT*60;
                  int hours = (i*Parameters.MIN_TIME_UNIT) / min_per_hour;
                  int minutes = (seconds / min_per_hour) % min_per_hour;
                  data[0][i] = hours + ":" + minutes;
                  data[1][i] = staffLevels.get(i).size();
              }

              dataset.addSeries(series1, data);

              return dataset;
        }
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • Hi @Klausos Klausos, I'm trying to use your example to answer my question (https://stackoverflow.com/questions/48145295/how-to-create-line-chart-that-starts-from-zero-and-1-axis-must-be-string-based?noredirect=1#comment83289662_48145295) but I got a compilation error message at the "data[0][1]" line. The error message is **"incompatible types: String cannot be converted to double"**. What should I do? Am I missing anything? – Anthony Jan 09 '18 at 03:32

1 Answers1

3

Use setDateFormatOverride() on the axis with a suitable DateFormat:

axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
trashgod
  • 203,806
  • 29
  • 246
  • 1,045