从CSV文件自动创建图表的工具(在ANT中)

时间:2011-09-15 11:42:03

标签: ant csv automation

我目前正在测试一个写入几个CSV文件的软件。现在我正在寻找一个免费的工具来自动生成该文件的图表(图像)。我正在使用eclipse,我正在使用ANT来构建我的项目。因此,将它与ANT一起使用的工具(可能在Java中)会很棒。

目前,我必须手动完成许多步骤:

  • 转到文件夹
  • 打开并编辑文件(替换。,以数字表示)
  • 在开放式办公室打开文件
  • 创建图表
  • 将其导出

有人知道如何自动化这个过程(甚至是它的一些步骤)吗?

3 个答案:

答案 0 :(得分:5)

选项1

用于生成图形的unix上一个非常有用的程序是gnuplot

我还找到了一个csv2gnuplot script,您可以在shell脚本中使用它来批处理文件。

选项2

如果您已经与ANT / Java解决方案结合(也许您正在使用Windows),那么我使用following example

调整了jfreechart

的src / sample.csv

Max Pause Goal,Minor Collections,Major Collections,Pause Count,Max Pause,GC Time,Total Time, Throughput
0,49,0,49,0.005,0.081,1.831,95.599
100,49,0,49,0.005,0.077,1.828,95.785
200,49,0,49,0.005,0.081,1.829,95.550
300,47,0,47,0.009,0.089,1.837,95.145
400,48,0,48,0.005,0.081,1.835,95.598
500,48,0,48,0.005,0.078,1.825,95.729
600,49,0,49,0.005,0.081,1.830,95.600
700,48,0,48,0.005,0.081,1.828,95.564
800,44,0,44,0.017,0.094,1.857,94.919
900,49,0,49,0.006,0.082,1.833,95.533
1000,49,0,49,0.005,0.088,1.840,95.224

的build.xml

<project name="demo" default="chart" xmlns:ivy="antlib:org.apache.ivy.ant">

    <property name="build.dir"   location="build"/>
    <property name="input.file"  location="src/sample.csv"/>
    <property name="output.file" location="${build.dir}/sample.png"/>

    <target name="init">
        <ivy:resolve/>
        <ivy:cachepath pathid="build.path" conf="build"/>

        <mkdir dir="${build.dir}"/>
    </target>

    <target name="chart" depends="init">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

        <groovy>
        import java.io.*;
        import java.util.StringTokenizer;

        import org.jfree.chart.*;
        import org.jfree.chart.plot.*;
        import org.jfree.data.xy.*;

        boolean SHOW_LEGEND = false;
        boolean SHOW_TOOLTIPS = false;
        boolean GENERATE_URLS = false;

        FileReader fr = new FileReader(properties["input.file"]);
        BufferedReader br = new BufferedReader(fr);

        // Get the x-axis label from the first token in the first line
        // and the y-axis label from the last token in the first line.
        String line = br.readLine();
        StringTokenizer st = new StringTokenizer(line, ",");
        String xLabel = st.nextToken();
        String yLabel = st.nextToken();
        while (st.hasMoreTokens()) yLabel = st.nextToken();

        String title = yLabel + " by " + xLabel;

        // Get the data to plot from the remaining lines.
        float minY = Float.MAX_VALUE;
        float maxY = -Float.MAX_VALUE;
        XYSeries series = new XYSeries("?");
        while (true) {
            line = br.readLine();
            if (line == null) break;
            st = new StringTokenizer(line, ",");

            // The first token is the x value.
            String xValue = st.nextToken();

            // The last token is the y value.
            String yValue = "";
            while (st.hasMoreTokens()) yValue = st.nextToken();

            float x = Float.parseFloat(xValue);
            float y = Float.parseFloat(yValue);
            series.add(x, y);

            minY = Math.min(y, minY);
            maxY = Math.max(y, maxY);
        }

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        JFreeChart chart = ChartFactory.createXYLineChart(
            title, xLabel, yLabel, dataset,
            PlotOrientation.VERTICAL,
            SHOW_LEGEND, SHOW_TOOLTIPS, GENERATE_URLS);

        XYPlot plot = chart.getXYPlot();
        plot.getRangeAxis().setRange(minY, maxY);

        int width = 500;
        int height = 300;
        ChartUtilities.saveChartAsPNG(new File(properties["output.file"]), chart, width, height);
        </groovy>
    </target>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

</project>

的ivy.xml

ivy插件使ANT能够与Maven存储库通信。

<ivy-module version="2.0">
    <info organisation="org.myspotontheweb" module="demo"/>
    <configurations defaultconfmapping="build->default">
        <conf name="build" description="ANT tasks"/>
    </configurations>
    <dependencies>
        <dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.2"/>
        <dependency org="jfree" name="jfreechart" rev="1.0.13"/>
        <dependency org="jfree" name="jcommon" rev="1.0.15" force="true"/>
    </dependencies>
</ivy-module>

注意: 最新版本的jfreechart对jcommon版本1.0.16(在Maven Central中不存在)有依赖性

答案 1 :(得分:2)

您可以使用XChart,因为从Chart文件创建CSV是自version 2.2.0以来的新功能。代码看起来像这样:

// import chart from a folder containing CSV files
Chart chart = CSVImporter.getChartFromCSVDir("./path/to/dir/with/csv/files/", DataOrientation.Columns, 600, 600);

// Show it
new SwingWrapper(chart).displayChart();

// Save it
BitmapEncoder.savePNG(chart, "./Sample_Chart.png");
BitmapEncoder.savePNGWithDPI(chart, "./Sample_Chart_300_DPI.png", 300);

CSV文件中的数据可以是水平或垂直的,每个CSV文件应包含单个系列的数据。

您也可以执行相反操作并将Chart数据导出为CSV文件:

CSVExporter.writeCSVColumns(chart.getSeriesMap().get(0), "./path/to/dir//");

答案 2 :(得分:1)

您可以使用Open Office API,还有一些samples for Java in the wiki