A command line script which uses microsoft excel to plot a graph from a CSV file.
This project is originally written by Antoine. This repository is just a fork of the original project made by him. I don't know him personally. I liked this work, it wasn't on github yet (to the best of my knowledge), so I created a repo here. Following is a narrative by the original author. In case of any comments or suggestions, go to the Author's site and to report any issues or request improvements, open issues here.
I do software prototyping using a wide range of programming language. I often have to generate data that changes over time which is mostly displayed in a grid format. However, that’s not the best way to understand how the data changes over time compared to plotting the data to a graph.
Most programming language I know does not offer a native library for plotting data to a graph. It is not an easy task. Most programming language often require external libraries for implementing the process and each library does not work the same way.
Excel is a native platform for parsing Comma-separated values (CSV) files and can be scripted to plot the result into an image.
The purpose of this script is to allows any programming language which generates data to plot the data into an image by saving the raw data to a CSV file and then using the power of Excel to plot the result into an image.
The following section lists the features of csvPlot script:
The script has some limitations which are explained here.
The resolution of the output image may be +- 1 pixels different than what is requested on command line. This is a limitation of how Excel processes image dimensions since it uses “points” as base unit and not actual pixels. A conversion from pixels to points must be calculated which may contains small accuracy error.
The only supported image format is PNG. It is still unknown if Excel actually support JPG for exporting graphs but PNG seems to be the perfect candidate since its a lossless compressed format.
It is expected that first row of each column contains the title of the column which will be used as the name of the plotted series within the graph.
The command for launching the script is as follows. The script must be called with a minimum of 6 command line arguments.
Name | Description |
---|---|
InputFile | Path of the input CSV file |
OutputFile | Path of the output PNG image |
Width | Width of the output image in pixels |
Height | Height of the output image in pixels |
Series #1, X column | X column index of first series |
Series #1, Y column | Y column index of first series |
Series #2, X column | X column index of second series |
Series #2, Y column | Y column index of second series |
Series #n, X column | ... |
Series #n, Y column | ... |
Note that column indice are 1-based and not 0-based. This means that column A is column 1 and not column 0.
The script is written in VBScript. Use the following to execute the script from a batch file (*.bat) :
@echo off
cscript //nologo "%~dp0csvPlot.vbs" pathtodemo.csv pathtodemo.png 800 600 1 2
pause
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:>cd /d C:TempcsvPlotdemo
C:TempcsvPlotdemo>cscript //Nologo csvPlot.vbs %cd%CarEngineModel.csv %cd%CarEngineModel.png 853 479 1 3 1 4 1 2 Loading input file C:TempcsvPlotdemoCarEngineModel.csv...
File load successful.
File has 4 columns.
Plotting series of columns 1 and 3...
Plotting series of columns 1 and 4...
Plotting series of columns 1 and 2...
File C:TempcsvPlotdemoCarEngineModel.png saved successfully.
C:TempcsvPlotdemo>
The following example show the Apple Share Prices closing value over the year 2015. The data is provided by Nasdaq here.
The CSV data can be downloaded here: Apple Share Prices over time (2015).csv
The following show a hypothetical car engine model which speed increase of decrease over time based on the feedback of the gas pedal.
The CSV data can be downloaded here: Car Engine Model.csv
The following show a graph of sin(), cos() and log() function in Excel.
The CSV data can be downloaded here: sin(x), cos(x) & log(x) function data.csv