Code Monkey home page Code Monkey logo

scala-chart's Introduction

Scala Chart Library

Build Status Maven Central Scaladoc Reference Status

scala-chart is a Scala library for creating and working with charts. It wraps JFreeChart, much like scala-swing does with the original javax.swing package. This project is released under the same license as JFreeChart to make them fully license-compatible. Checkout the API by clicking on the scaladoc badge above.

Usage

Add the following to your sbt build:

libraryDependencies += "com.github.wookietreiber" %% "scala-chart" % "latest.integration"

In case exporting to PDF is required, also add iText to your dependencies:

libraryDependencies += "com.itextpdf" % "itextpdf" % "5.5.6"

In case exporting to SVG is required, also add JFreeSVG to your dependencies:

libraryDependencies += "org.jfree" % "jfreesvg" % "3.0"

Imports

All high-level convenience can be imported with the all you can eat import:

import scalax.chart.api._

For more and more a la carte imports, have a look at the module package for various selfless traits. There is also a module containing everything the api import does which can be used in applications directly:

object MyChartApp extends App with scalax.chart.module.Charting {
  val data = for (i <- 1 to 5) yield (i,i)
  val chart = XYLineChart(data)
  chart.saveAsPNG("/tmp/chart.png")
}

Creating Charts

Creating charts is as simple as using one of the many chart factories, which differ from the JFreeChart ones in the aspect, that they make heavy use of default arguments, so you have to type as less as possible:

val data = for (i <- 1 to 5) yield (i,i)
val chart = XYLineChart(data)

The first argument is always the dataset which is the only required argument. For better readability of your own code, you should name the other arguments:

val chart = XYLineChart(data, title = "My Chart of Some Points")

There are also some enrichments for the charts themselves to display them in a window or save them to disk:

chart.show()
chart.saveAsPNG("/tmp/chart.png")
chart.saveAsJPEG("/tmp/chart.jpg")
chart.saveAsPDF("/tmp/chart.pdf")
chart.saveAsSVG("/tmp/chart.svg")

Animations / Live Chart Updates

You can also do some animations, i.e. perform live updates on your datasets:

val series = new XYSeries("f(x) = sin(x)")
val chart = XYLineChart(series)
chart.show()
for (x <- -4.0 to 4 by 0.1) {
  swing.Swing onEDT {
    series.add(x,math.sin(x))
  }
  Thread.sleep(50)
}

scala-chart's People

Contributors

ceilican avatar dainkaplan avatar ramn avatar ruippeixotog avatar sciss avatar sylvainlb avatar wookietreiber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scala-chart's Issues

Q: Scatter Plot

What's the recommended way to use JFreeChart's FastScatterPlot from within scala-chart?

2.9.2 artifact

Would it be possible to publish an 2.9.2 artifact?

Thanks

Events

Hi,
It is possible to use events on chart like as the components from swing?

org.jfree.chart.axis.CategoryAxis3D Not found

code as below:

import scalax.chart.api._

val chart = BarChart(data)

will throw:

java.lang.NoClassDefFoundError: org/jfree/chart/axis/CategoryAxis3D
  at scalax.chart.api$.BarChart$lzycompute$1(api.scala:7)
  at scalax.chart.api$.BarChart(api.scala:7)
  ... 36 elided
Caused by: java.lang.ClassNotFoundException: org.jfree.chart.axis.CategoryAxis3D
  at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
  ... 38 more

sbt as :
libraryDependencies += "com.github.wookietreiber" %% "scala-chart" % "0.5.1"

Dependency on itext

After adding only

 libraryDependencies += "com.github.wookietreiber" %% "scala-chart" % "latest.integration"

I get this compilation error:

Error:scalac: missing or invalid dependency detected while loading class file 'PDFExporter.class'.
Could not access term itextpdf in package com,
because it (or its dependencies) are missing. Check your build definition for  
missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'PDFExporter.class' was compiled against an incompatible version of com.

It works after adding also the itext dependency, but in the documentation is seems that this latter one is not needed

Set transparent background

I'm trying to change the theme of my chart so that it gets a transparent background, but nothing seems to apply.

I've tried:

val transparent = new Color(0xFF, 0xFF, 0xFF, 0)
implicit val theme: StandardChartTheme =
    org.jfree.chart.StandardChartTheme.createLegacyTheme().asInstanceOf[StandardChartTheme]
theme.setPlotBackgroundPaint(transparent)
theme.setChartBackgroundPaint(transparent)
theme.setLegendBackgroundPaint(transparent)

Am I doing something wrong?

0.3.0 release

Hi! Would it be possible to publish the current version as 0.3.0 release, and continue with 0.4.0-SNAPSHOT after that? I have a few libraries depending on 0.3.0-SNAPSHOT, and it would be great if I could publish stable versions of those, too. Thanks.

chart.show opens tiny window that must be manually expanded

Hi, when I follow the example on in README.md, the window that pops up is extremely small. It is so small that I didn't even find the window the first time that it opened up! I must manually drag the edges to be able to view the window contents. Is there a way to supply a dim argument to chart.show the same that that can be done with saveAsPDF?

Thanks!

Log axis

Hi,
I do not see the option settings for XYChart log axis. There is a possibility in this release or require a longer writing in Java?

I'm trying to use this lib + scala 2.11

i'm using the scala-chart jar 2.10_-0.2.2.jar in Eclipse with Scala-ide plugin.
The compiler is telling me:
_

scala-chart_2.10-0.2.2.jar of Sample Scala 2 build path is cross-compiled with an incompatible version of Scala (2.10.0). In case this report is mistaken, this check can be disabled in the compiler

_

Is there an update available?? How can I work around this?

How to draw a chart with points instead of lines?

Is there any class/object I can use to create chart with points but without lines?

I manage to do it by:

val chart = XYLineChart(series)
val renderer = new XYLineAndShapeRenderer(false, true)
chart.plot.setRenderer(renderer)

But is there any better way?

And how to manipulate size of point's shape?

3d charts?

Is it possible to display 3d points on a plot?

Export to SVG

http://www.jfree.org/jfreesvg/

JFreeSVG is a fast, light-weight, vector graphics library for the Java(tm) platform that makes it easy to generate graphical output in SVG format directly from Java code. The library contains:
SVGGraphics2D - generate Scalable Vector Graphics (SVG) output via standard Java2D API calls;
CanvasGraphics2D - generate Javascript code that draws to an HTML5 Canvas object;

multi-line plotting

Is it possible to plot 2 horizontal lines (y=cte1, y=cte2) and a series value in the same chart?

Building a histogram or Category chart

I'm more familiar with R; and have been struggling to figure out how to make a simple histogram/category chart.

Lets say I have a sequence of probabilities, trials, represented as a Seq[Int] in Scala, or numeric vector in R.

I've tried val chart = CategoryChart(trials, "trials", legend = false), but can't seem to get any results.

In R, I would use hist(trials, xlab="x-axis", ylab="y-axis")

Charts blurry on Retina display

When charts are shown in high-density displays such as Retina displays in MacBooks, the fonts and the lines look blurry.

There seems to be a simple fix for that by disabling the ChartPanel buffer, but that will surely affect drawing performance (I don't know to what extent, though). Nonetheless, scala-chart should either support a better solution to draw charts on Retina displays or expose the useBuffer flag to let users decide if they want to trade quality by performance.

Modify x-axis range

Hi @wookietreiber,
It is possible to customize the x-axis values? I am generating a XYLineChart with values of a whole week hours (180 values). Instead of showing 10-10 intervals I would like to show hours and ideally also days, something like:

1,2,3,...23,24,1,2,3...,23,24,....
Monday Tuesday...

X-Y data creation : not all types work

For X values, these types do not work:

  • java.lang.Long
  • java.math.BigInteger
// fail to compile:
val d1 = for (i <- 1 to 5) yield (new java.lang.Long(i), i.toDouble)
val d2 = for (i <- 1 to 5) yield (new java.lang.Long(i), i)
val d3 = for (i <- 1 to 5) yield (new java.math.BigInteger(i.toString), i.toDouble)

These work:

// compiles:
 val data1 = for (i <- 1 to 5) yield (new java.lang.Integer(i), i)
 val data2 = for (i <- 1 to 5) yield (new java.lang.Double(i), i)
 val data3 = for (i <- 1 to 5) yield (new java.lang.Float(i), i)
 val data4 = for (i <- 1 to 5) yield (i.toDouble,i.toDouble)
 val data5 = for (i <- 1 to 5) yield (new BigInt(new java.math.BigInteger(i.toString)), i.toDouble)
 val data6 = for (i <- 1 to 5) yield (i.toLong, i) ; XYLineChart(data).show()
 val data7 = for (i <- 1 to 5) yield (new java.util.Date(new java.lang.Long(i)), i)

CategoryLabelGenerator throws exception: org.jfree.data.UnknownKeyException

The CategoryLabelGenerator seems to throw an exception when used. This is reproducible with the code below:

  import scalax.chart.api._
  import org.jfree.data.category.DefaultCategoryDataset
  def barChart(multiseries: (String, Seq[(String,Double)])*): CategoryChart = {
    val ds = new DefaultCategoryDataset
    for {
      (setLbl, series) <- multiseries
      (seriesLbl, v) <- series
    } ds.addValue(v, seriesLbl, setLbl)
    val c = BarChart(ds)
    c.labelGenerator = CategoryLabelGenerator.Default
    c
  }
  barChart("Set 1" -> List("A" -> 3d, "B" -> 1d), "Set 2" -> List("A" -> 2d, "B" -> 2d))

This throws the following exception:

org.jfree.data.UnknownKeyException: Unrecognised columnKey: 0
    at org.jfree.data.DefaultKeyedValues2D.getValue(DefaultKeyedValues2D.java:274)
    at org.jfree.data.category.DefaultCategoryDataset.getValue(DefaultCategoryDataset.java:217)
    at scalax.chart.module.CategoryLabelGenerators$CategoryLabelGenerator$$anonfun$Default$1.apply(CategoryLabelGenerators.scala:76)
    at scalax.chart.module.CategoryLabelGenerators$CategoryLabelGenerator$$anonfun$Default$1.apply(CategoryLabelGenerators.scala:76)
    at scalax.chart.module.CategoryLabelGenerators$CategoryLabelGenerator$$anon$1.generateLabel(CategoryLabelGenerators.scala:70)

Looking at the source, the problem is actually in fromPeer() and toPeer(). CategoryLabelGenerator expects the row and category labels, but in toPeer() it uses the series and category ids instead; since all of these are Comparables it compiles regardless.

    final def fromPeer(jfree: JCategoryLabelGenerator): CategoryLabelGenerator =
      CategoryLabelGenerator { (dataset, series, category) =>
        // XXX: Uses series and category as keys to look up indices:
        val row = dataset.getRowIndex(series)
        val col = dataset.getColumnIndex(category)
        jfree.generateLabel(dataset, row, col)
      }

    final def toPeer(generator: CategoryLabelGenerator): JCategoryLabelGenerator = new JCategoryLabelGenerator {
      override final def generateColumnLabel(dataset: CategoryDataset, category: Int): String = ""

      // XXX: This is passing in ints for series and category, but as can be seen above
      //      in fromPeer, it uses them as the keys to get the index (even though we 
      //      already have it).
      override final def generateLabel(dataset: CategoryDataset, series: Int, category: Int): String =
        generator(dataset, series, category) 

      override final def generateRowLabel(dataset: CategoryDataset, series: Int): String = ""
    }

I have a (1 line) fix that converts the indices back to keys before calling the CategoryLabelGenerator: fbab9bf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.