Created
May 24, 2021 07:48
-
-
Save jamesdavidson/974ae1bccc4d2074587ca78616799964 to your computer and use it in GitHub Desktop.
scatterplot.clj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; clj -Sdeps '{:deps {org.apache.poi/poi {:mvn/version "4.1.1"} org.apache.poi/poi-ooxml {:mvn/version "4.1.1"}}}' | |
;; based on https://stackoverflow.com/questions/59061235/apache-poi-manage-scatter-chart | |
(ns user | |
(:import [org.apache.poi.xssf.usermodel XSSFWorkbook] | |
[org.apache.poi.ss.util CellRangeAddress] | |
[org.apache.poi.xddf.usermodel.chart XDDFDataSourcesFactory ChartTypes AxisCrosses AxisPosition MarkerStyle] | |
[org.apache.poi.ss.usermodel.charts LegendPosition] | |
[java.io FileOutputStream])) | |
(def wb (new XSSFWorkbook)) | |
(def sheet (.createSheet wb "Sheet 1")) | |
(def NUM_OF_ROWS 3) | |
(def NUM_OF_COLUMNS 10) | |
(dorun | |
(for [i (range NUM_OF_ROWS)] | |
(.createRow sheet i))) | |
(dorun | |
(for [i (range NUM_OF_ROWS) | |
j (range NUM_OF_COLUMNS)] | |
(let [row (.getRow sheet i) | |
cell (.createCell row j)] | |
(.setCellValueImpl cell (* j (inc i)))))) | |
(def drawing (.createDrawingPatriarch sheet)) | |
(def anchor (.createAnchor drawing 0, 0, 0, 0, 0, 5, 10, 15)) | |
(def chart (.createChart drawing anchor)) | |
(def legend (.getOrCreateLegend chart)) | |
(.setPosition legend LegendPosition/TOP_RIGHT) | |
(def bottomAxis (.createValueAxis chart AxisPosition/BOTTOM)) | |
(.setTitle bottomAxis "x") | |
(def leftAxis (.createValueAxis chart AxisPosition/LEFT)) | |
(.setTitle leftAxis "f(x)") | |
(.setCrosses leftAxis AxisCrosses/AUTO_ZERO) | |
(def xs (XDDFDataSourcesFactory/fromNumericCellRange sheet (new CellRangeAddress 0, 0, 0, (dec NUM_OF_COLUMNS)))) | |
(def ys1 (XDDFDataSourcesFactory/fromNumericCellRange sheet (new CellRangeAddress 1, 1, 0, (dec NUM_OF_COLUMNS)))) | |
(def ys2 (XDDFDataSourcesFactory/fromNumericCellRange sheet (new CellRangeAddress 2, 2, 0, (dec NUM_OF_COLUMNS)))) | |
(def data (.createData chart ChartTypes/SCATTER bottomAxis leftAxis)) | |
(def series1 (.addSeries data xs, ys1)) | |
(.setTitle series1 "2x" nil) ; // https://stackoverflow.com/questions/21855842 | |
(.setSmooth series1 false) ; // https://stackoverflow.com/questions/39636138 | |
(.setMarkerStyle series1 MarkerStyle/CIRCLE) | |
(.setMarkerSize series1 5) | |
(def series2 (.addSeries data xs ys2)) | |
(.setTitle series2 "3x" nil) | |
(.setMarkerStyle series2 MarkerStyle/CIRCLE) | |
(.setMarkerSize series2 5) | |
(.plot chart data) | |
(with-open [os (new FileOutputStream "ooxml-scatter-chart.xlsx")] | |
(.write wb os)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment