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

Tho*_*rig 2 csv ant automation

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

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

  • 转到文件的文件夹
  • 打开并编辑文件(替换.,以数字表示)
  • 在开放式办公室打开文件
  • 创建一个图表
  • 出口它

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

Mar*_*nor 5

选项1

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

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

选项2

如果您已经与ANT/Java解决方案结合(也许您正在使用Windows),那么我使用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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

的ivy.xml

常春藤插件使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>
Run Code Online (Sandbox Code Playgroud)

注意: 最新版本的jfreechart对jcommon 1.0.16版本的依赖性有所下降(Maven Central中没有)