无法解析类型org.jfree.data.general.Series

时间:2016-08-02 14:10:23

标签: jfreechart m2eclipse eclipse-neon

我一直在将我的开发环境更新到Eclipse Neon并引入m2e来管理项目依赖项。在一个项目中,JFreeChart的原始(非常旧)版本在maven上不可用,因此我添加了最新版本的JFreeChartJCommon

我现在有一个编译错误,如下所述:

The type org.jfree.data.general.Series cannot be resolved.
It is indirectly referenced from required .class files.

我已经找到了许多关于此的问题和答案,但它们似乎与JCommon的缺席有关。我的依赖项中有JCommon,我已经检查了所引用的特定类实际上是带有7-zip的jfreechart.jar文件。 下面是带有错误的代码片段,它出现在最后一个else语句results.add(…)上。

public XYDataset getMetricHistogramData(Timestamp t1, Timestamp t2){
    int index1=-1;
    Timestamp startTime,endTime;
    TimePeriodValues results = new TimePeriodValues(this.type.name());
    final TimePeriodValuesCollection results1 = new TimePeriodValuesCollection();

    if (t1.before(earliest)||t1.after(latest)||(readings.size()<=1)) return null; // won't find a value for the timestamp t1
    if (t2.before(earliest)||t2.after(latest)) return null; // won't find a value for the timestamp t2

    for(int i = 0;i<readings.size();i++){
        if (readings.get(i).timestamp().equals(t1)){
            index1=i;
            break;
        }
        if (readings.get(i).timestamp().after(t1)){
            index1=i-1;
            break;
        }       
    }
    // index1 now contains the index of the starting timestamp
    for (int i=index1; i<(readings.size()-1); i++){
        startTime = readings.get(i).timestamp();
        if(startTime.after(t2)) break;
        //endTime = new Timestamp(Math.abs(readings.get(i+1).timestamp().getTime()-Timestamped.SECOND_IN_MS));
        endTime = readings.get(i+1).timestamp();
        if (endTime.before(startTime))
            SmartPower.getMain().getFrame().displayLog("Bad Timestamps "+startTime + " - " + endTime+"\n\r");
        else results.add(new SimpleTimePeriod(startTime, endTime), readings.get(i).value());
    }
    results1.addSeries(results);
    return results1;
}

这是我的pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SmartPower</groupId>
  <artifactId>SmartPower</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>javax.persistence</artifactId>
      <version>2.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
    <dependency>
      <groupId>org.jfree</groupId>
      <artifactId>jfreechart</artifactId>
      <version>1.0.19</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.jfree/jcommon -->
    <dependency>
      <groupId>org.jfree</groupId>
      <artifactId>jcommon</artifactId>
    <version>1.0.23</version> 
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.jgoodies/forms -->
    <dependency>
      <groupId>com.jgoodies</groupId>
      <artifactId>forms</artifactId>
      <version>1.2.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.derby/derby -->
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derby</artifactId>
      <version>10.12.1.1</version>
</dependency>

  </dependencies>
</project>

Neon在一周前在Windows 10上安装得很干净。

任何指针都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

我不确定哪里出了问题。班级org.jfree.data.general.Series确实包含在jfreechart-1.0.19.jar中。您可以尝试使用-verbose选项从命令行运行,如建议的here,以检查杂散JAR。使用1.0.19 release

$ java -verbose -cp .:lib/jfreechart-1.0.19.jar:lib/jcommon-1.0.23.jar Test
…
[Loaded org.jfree.data.general.SeriesDataset from file:lib/jfreechart-1.0.19.jar]
[Loaded org.jfree.data.xy.XYDataset from file:lib/jfreechart-1.0.19.jar]
…

使用引用的版本,以下简化示例生成以下图表而不出错:

image

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimePeriodValues;
import org.jfree.data.time.TimePeriodValuesCollection;
import org.jfree.data.xy.XYDataset;

/**
 * @see https://stackoverflow.com/a/38725689/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(ChartFactory.createTimeSeriesChart(
            "Title", "Domain", "Range", createDataset())) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private XYDataset createDataset() {
        TimePeriodValues results = new TimePeriodValues("Data");
        results.add(new Day(1, 8, 2016), 1);
        results.add(new Day(2, 8, 2016), 2);
        return new TimePeriodValuesCollection(results);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}