I have JSON file that looks something like this
{
"data": [
{
"type": "SENIOR",
"male": 350000.0,
"female": 225000.0
},
{
"type": "ENTRY",
"male": 100000.0,
"female": 40000.0
},
{
"type": "MID",
"male": 225000.0,
"female": 150000.0
}
]
}
and I need to create a bar plot with dynamic x fields, that are read from json.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{luacode}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{ifthen}
%%%%% Lua code %%%%%%
\begin{luacode}
local json = require("json")
local file = io.open("mwe.json")
tab = json.parse(file:read("*all"))
file:close()
\end{luacode}
\begin{document}
% Number of labels
\newcommand{\nrlabels}{\directlua{tex.print(table.getn(tab['data']))}}
% Create counter for loop through labels
\newcounter{labelctr}\stepcounter{labelctr}
\newcounter{totallabels}
\setcounter{totallabels}{\nrlabels}\stepcounter{totallabels}
\newcounter{onemorelabel}\setcounter{onemorelabel}{\value{totallabels}}\addtocounter{onemorelabel}{-1}
\begin{tikzpicture}
\begin{axis}[
width = 0.85\textwidth,
height = 8cm,
%major x tick style = transparent,
ybar=2\pgflinewidth,
bar width=20pt,
%ymajorgrids = true,
ylabel = {Average Wage},
%symbolic x coords= {\whiledo{\value{labelctr} < \value{totallabels}}{\directlua{tex.print(tab['data'][\thelabelctr]['type'])}\ifthenelse{\value{labelctr}<\value{onemorelabel}}{,}{}\stepcounter{labelctr}}}
symbolic x coords={SENIOR,ENTRY,MID}
]
\addplot[fill=blue] coordinates {\whiledo{\value{labelctr} < \value{totallabels}}{(\directlua{tex.print(tab['data'][\thelabelctr]['type'])}, \directlua{tex.print(tab['data'][\thelabelctr]['male'])}) \stepcounter{labelctr}}};
\end{axis}
\end{tikzpicture}
\end{document}
This is what I have so far.
And I need something like this
Thanks..
