Skip to content

Commit

Permalink
Add pie chart
Browse files Browse the repository at this point in the history
Added pie chart and donut chart as a POC, to make sure I understand how
to create visualizations using the type system. Some changes were made
to add_data! such that character data can now be provided
  • Loading branch information
randyzwitch committed May 21, 2015
1 parent 1892160 commit d4dee4f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ A Julia package for creating the simplest kinds of Vega visualizations. We curre
* Bar plots
* Line plots
* Scatter plots
* Pie/Donut charts

In addition, this package wraps Vega's pseudo-type system inside of Julia types. This will eventually provide tools for manipulating graphics with a higher level of control over layout.

Expand All @@ -26,7 +27,7 @@ This package depends upon [Vega.js](https://github.com/trifacta/vega) from Trifa

# Usage Examples

The current API provides some convenience wrappers around Vega for generating standard kinds of plots. These are described below. If you have any issues with the examples, please try checking out master for `Distributions`, `Grid`, and `KernelDensity`. While these three packages aren't requirements to use Vega.jl, they are used for generating data to plot in the examples.
The current API provides some convenience wrappers around Vega for generating standard kinds of plots. These are described below. If you have any issues with the examples, please try checking out master for `Distributions`, `Grid`, `Optim` and `KernelDensity`. While these three packages aren't requirements to use Vega.jl, they are used for generating data to plot in the examples.

using Distributions
using Vega
Expand Down Expand Up @@ -98,3 +99,13 @@ The current API provides some convenience wrappers around Vega for generating st
heatmap(x = x, y = y, group = color)

![Example 8](content/heatmap.png)

fruit = ["peaches", "plums", "blueberries", "strawberries", "bananas"]
bushels = [100, 32, 180, 46, 21]
plot(x = fruit, y = bushels, kind = :pie)

![Example 9](content/pie.png)

plot(x = fruit, y = bushels, kind = :donut)

![Example 10](content/donut.png)
Binary file added content/donut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/pie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ for i in 1:n
end
end
heatmap(x = x, y = y, group = color)

fruit = ["peaches", "plums", "blueberries", "strawberries", "bananas"]
bushels = [100, 32, 180, 46, 21]
plot(x = fruit, y = bushels, kind = :pie)
plot(x = fruit, y = bushels, kind = :donut)
3 changes: 2 additions & 1 deletion src/Vega.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Vega
export tojson, tojs

export plot
export barplot, lineplot, scatterplot, areaplot, heatmap
export barplot, lineplot, scatterplot, areaplot, heatmap, piechart, donutchart

export xlab!, ylab!, xlim!, ylim!, title!, legend!

Expand Down Expand Up @@ -55,6 +55,7 @@ module Vega
include("derived/scatterplot.jl")
include("derived/areaplot.jl")
include("derived/heatmap.jl")
include("derived/piechart.jl")


end
32 changes: 32 additions & 0 deletions src/derived/piechart.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function piechart(; x::AbstractArray = Real[], y::AbstractArray = [], holesize::Int = 0)

v = VegaVisualization()
v.legends = [VegaLegend(fill = "color", title = "group")]

add_data!(v, x = x, y = y)

v.scales = [VegaScale(name = "color",
domain = VegaDataRef("table", "data.x"),
range = "category10",
_type = "ordinal")]

v.marks = [VegaMark(_type = "arc",
from = Dict{Any, Any}("data"=> "table", "transform"=> [Dict{Any, Any}("type"=> "pie", "value"=> "data.y")]),
properties = VegaMarkProperties(enter = VegaMarkPropertySet(
endAngle = VegaValueRef(field = "endAngle"),
fill = VegaValueRef(field = "data.x", scale = "color"),
innerRadius = VegaValueRef(value = holesize),
outerRadius = VegaValueRef(value = 250),
startAngle = VegaValueRef(field = "startAngle"),
stroke = VegaValueRef(value = "white"),
x = VegaValueRef(group = "width", mult = 0.5),
y = VegaValueRef(group = "height", mult = 0.5)
)
)
)
]

v
end

donutchart(;x::AbstractArray = Real[], y::AbstractArray = [], holesize::Int = 175) = piechart(x = x, y = y, holesize = holesize)
4 changes: 4 additions & 0 deletions src/derived/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function plot(;x::AbstractVector = Int[],
elseif kind == :hist
a, b = hist(x)
v = plot(x = [a][2:end], y = b, kind = :bar)
elseif kind == :pie
v = piechart(x = x, y = y)
elseif kind == :donut
v = donutchart(x = x, y = y)
else
error("Unknown kind of plot")
end
Expand Down
8 changes: 4 additions & 4 deletions src/intermediates/data.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function add_data!{R <: Real,
S <: Real,
T <: Real}(v::VegaVisualization;
function add_data!{R <: Any,
S <: Any,
T <: Any}(v::VegaVisualization;
x::AbstractVector{R} = Int[],
y::AbstractVector{S} = Int[],
group::AbstractVector{T} = Int[])
Expand Down Expand Up @@ -32,4 +32,4 @@ function add_data!{R <: Real,
end

return v
end
end

0 comments on commit d4dee4f

Please sign in to comment.