import io.data2viz.charts.*
import io.data2viz.charts.core.*
import io.data2viz.charts.dimension.*
import io.data2viz.charts.chart.*
import io.data2viz.charts.chart.mark.*
import io.data2viz.charts.viz.*
import io.data2viz.charts.layout.*
import io.data2viz.geom.*
val width = 700.0
val height = 500.0
enum class ProfitType {
Corporate, Domestic, Financial, NonFinancial
}
data class Profits(val year: Int, val corporate: Double, val domestic: Double, val financial: Double, val nonFinancial: Double)
data class SplitProfit(val year: Int, val type: ProfitType, val value: Double)
fun main() {
// Creating and sizing the VizContainer
val vc = newVizContainer().apply {
size = Size(width, height)
}
vc.chart(splitProfits) {
title = "Profits by categories"
// Create a discrete dimension for the years
val year = discrete( { domain.year } )
// Create a continuous numeric dimension for the profits values
val profit = quantitative( { domain.value } ) {
name = "Value in billions of USD (not adjusted for inflation)"
}
series = discrete( { domain.type } )
// Activate stacking
area(year, profit) {
stacking = Stacking.Standard
}
}
}
val profits = listOf(
Profits(1934,2.4,2.3,0.5,1.8),
Profits(1935,3.9,3.7,0.5,3.2),
Profits(1936,6.0,5.9,0.9,5.1),
Profits(1937,6.9,6.6,0.8,5.8),
Profits(1938,4.8,4.5,0.9,3.6),
Profits(1939,6.4,6.1,0.9,5.2),
Profits(1940,9.6,9.4,1.1,8.3),
Profits(1941,15.3,14.9,1.1,13.7),
Profits(1942,20.4,20.1,1.2,18.9),
Profits(1943,24.7,24.4,1.3,23.0),
Profits(1944,24.8,24.4,1.6,22.8),
Profits(1945,20.2,19.9,1.6,18.2),
Profits(1946,17.9,17.2,2.1,15.2),
Profits(1947,23.5,22.5,1.7,20.8),
Profits(1948,30.9,29.6,2.5,27.1),
Profits(1949,28.7,27.6,3.1,24.5),
Profits(1950,35.6,34.4,3.1,31.3),
Profits(1951,40.8,39.1,3.4,35.6),
Profits(1952,38.8,37.0,4.1,32.8),
Profits(1953,39.1,37.3,4.6,32.8),
Profits(1954,38.3,36.4,4.8,31.6),
Profits(1955,49.0,46.6,5.1,41.6),
Profits(1956,48.0,45.2,5.4,39.8),
Profits(1957,47.6,44.5,5.7,38.8),
Profits(1958,42.5,39.9,6.1,33.8),
Profits(1959,53.8,51.1,7.3,43.8),
Profits(1960,53.1,50.0,8.2,41.7),
Profits(1961,54.2,50.9,8.2,42.7),
Profits(1962,62.3,58.6,8.5,50.1),
Profits(1963,68.3,64.3,8.1,56.1),
Profits(1964,75.5,71.1,8.7,62.4),
Profits(1965,86.5,81.8,9.2,72.7),
Profits(1966,92.5,88.0,10.5,77.5),
Profits(1967,90.2,85.4,11.0,74.4),
Profits(1968,97.3,91.6,12.7,78.9),
Profits(1969,94.5,87.9,13.5,74.4),
Profits(1970,82.5,75.4,15.2,60.2),
Profits(1971,96.1,88.2,17.4,70.8),
Profits(1972,111.4,101.9,19.1,82.8),
Profits(1973,124.5,109.6,20.7,88.9),
Profits(1974,115.1,97.7,20.2,77.5),
Profits(1975,133.3,118.7,19.7,98.9),
Profits(1976,161.6,145.1,24.1,121.0),
Profits(1977,191.8,172.7,30.9,141.9),
Profits(1978,218.4,195.6,38.8,156.8),
Profits(1979,225.4,190.7,39.0,151.8),
Profits(1980,201.4,166.0,31.2,134.7),
Profits(1981,223.3,193.6,26.8,166.8),
Profits(1982,205.7,173.1,22.9,150.2),
Profits(1983,259.8,224.8,33.6,191.2),
Profits(1984,318.6,282.0,32.2,249.8),
Profits(1985,332.5,294.4,44.8,249.6),
Profits(1986,314.1,274.7,55.2,219.5),
Profits(1987,367.8,319.8,59.9,259.9),
Profits(1988,426.6,369.6,65.4,304.3),
Profits(1989,425.6,358.4,74.9,283.5),
Profits(1990,434.4,358.4,81.6,276.7),
Profits(1991,457.3,380.8,109.5,271.3),
Profits(1992,496.2,423.1,127.1,296.1),
Profits(1993,543.7,466.8,119.3,347.5),
Profits(1994,628.2,550.3,116.8,433.5),
Profits(1995,716.2,623.2,152.7,470.6),
Profits(1996,801.5,699.5,162.1,537.4),
Profits(1997,884.8,777.3,191.1,586.2),
Profits(1998,812.4,709.7,165.85,543.8),
Profits(1999,856.3,734.8,200.75,534.05),
Profits(2000,819.2,673.6,203.75,469.85),
Profits(2001,784.2,614.5,244.4,370.1),
Profits(2002,872.2,714.3,287.1,427.2),
Profits(2003,977.8,812.0,325.9,486.1),
Profits(2004,1246.9,1041.9,364.4,677.5),
Profits(2005,1456.1,1216.6,419.0,797.6),
Profits(2006,1608.3,1351.5,427.6,923.9),
Profits(2007,1510.6,1159.8,323.8,835.9),
Profits(2008,1248.4,841.8,86.8,755.0),
Profits(2009,1342.3,989.5,328.9,660.6),
Profits(2010,1702.4,1300.9,383.7,917.1),
Profits(2011,1827.0,1388.1,381.0,1007.1)
)
val splitProfits = profits.map {
listOf(
SplitProfit(it.year, ProfitType.Corporate, it.corporate),
SplitProfit(it.year, ProfitType.Domestic, it.domestic),
SplitProfit(it.year, ProfitType.Financial, it.financial),
SplitProfit(it.year, ProfitType.NonFinancial, it.nonFinancial)
)
}.flatten()