#!/usr/bin/env python
# coding: utf-8

# # Combine figures from two Statbank tabeles in one chart
# ## Example CPI Total and CPI-ATE
# Jupyter Notebook version at https://github.com/janbrus/ssb-api-python-examples

# Combine data from Statistics Norway's Statbank tables [03013](https://www.ssb.no/en/statbank/table/03013) and [05327](https://www.ssb.no/en/statbank/table/05327)
# ![cpi-ate.png](attachment:cpi-ate.png)

#Import packages
import matplotlib.pyplot as plt
import requests
import pandas as pd
from pyjstat import pyjstat  # pyjstat is for the JSON-stat format


# URLs to table metdata, where we post our queries.
URL1 = 'https://data.ssb.no/api/v0/en/table/05327' # KPI-jae
URL2 = 'https://data.ssb.no/api/v0/en/table/03013' # KPI total

### **** API queries start ****  ###
# API query towards table 05327 - latest 5 years, using filter "top"
q1 = {
  "query": [
    {
      "code": "Konsumgrp",
      "selection": {
        "filter": "item",
        "values": ['JA_TOTAL', 'JAE_TOTAL', 'JE_TOTAL', 'JEL_TOTAL']
      }
    },
    {
      "code": "ContentsCode",
      "selection": {
        "filter": "item",
        "values": ["KPIJustIndMnd"]
      }
    },
    {
      "code": "Tid",
      "selection": {
        "filter": "top",
        "values": [ "60" ]
      }
    }
  ],
  "response": {
    "format": "json-stat2"
  }
}

# API query in JSON towards table 03013 latest 5 years
q2 = {
  "query": [
    {
      "code": "Konsumgrp",
      "selection": {
        "filter": "item",
        "values": ["TOTAL"]
      }
    },
    {
      "code": "ContentsCode",
      "selection": {
        "filter": "item",
        "values": ["KpiIndMnd"]
      }
    },
    {
      "code": "Tid",
      "selection": {
        "filter": "top",
        "values": ["60"]
      }
    }
  ],
  "response": {
    "format": "json-stat2"
  }
}
### **** API queries end ****  ###

# Post queries q1 and q2 towards metadata URLs. Results are saved as res1 og res2
res1 = requests.post(URL1, json=q1)
res2 = requests.post(URL2, json=q2)

# Reads JSON-stat result with the library pyjstat
ds1 = pyjstat.Dataset.read(res1.text)
ds2 = pyjstat.Dataset.read(res2.text)

# Write to to Panda dataframes, df1 og df2.
df1 = ds1.write('dataframe')
df2 = ds2.write('dataframe')

# Exploring
df1.head(7)
df1.tail() # end of dataset
df2.tail(15)

# Try plot
df2.plot()  # here it is only one series

df1.plot() # standard plot shows alle 4 intervals as one line

# ### Combines the two dataframes  df1 og df2 using simple concat to the dataset "together"
together = pd.concat([df1,df2])

# Shows top and tail of the combined dataset
together.head()
together.tail()

# ### Pivot table for better view

# ["tidy data" Hadley Wickham](https://vita.had.co.nz/papers/tidy-data.pdf)

df3 = together.pivot(index='month', columns='consumption group', values='value')

df3.head(3)

# Use Pandas PeriodIndex
df3.index = pd.PeriodIndex(df3.index.str.replace("M", "-"), freq='M')

df3.tail(3)

# ### Chart made with Pandas' own plot-function
df3.plot()

# Plot with more parametres
df3.plot(marker="o", markersize=3, figsize=(12, 8))

# Saves display of the chart as a function. Here you can enter heght and width as parameters
def visfigur(bredde=12, hoyde=6):
    fig, ax = plt.subplots(figsize=(bredde, hoyde))
    #define ssb colors for chart
    ssbCol = ['#1a9d49', '#075745', '#1d9de2', '#0f2080', '#c78800', '#471f00', '#c775a7', '#a3136c', '#909090', '#000000']
    plt.xlabel('Month')
    plt.ylabel('index')
    ax.set_title('Chart showing CPI Total and underlying CPI series')
    df3.plot(ax=ax, color=ssbCol)
    plt.show()

# ### Plot using different styles
# In Matplotlib you can try out readymade styles. It os also possible to make one for Statistics Norway's design.
#
# Try out: 'Solarize_Light2',  'bmh', 'classic', 'dark_background' 'fivethirtyeight', seaborn-talk

with plt.style.context('Solarize_Light2'):
    visfigur()

with plt.style.context('fivethirtyeight'):
    visfigur(16, 9)

