# Stacked chart in TeX (added up line loads)

I've got a problem with finding the correct way or commands to make a nice plot in TeX, even with some basic experience in pgf/TikZ.

Imagine a weight distribution with start- and stop values and a line load, given in CSV-data with about 150 rows. For example something like this:

x-start [m] | x-stop [m] | weight [t/m]
-3;   5; 2.5
2;   4 ; 5.6
2.45;  6.33 ; 0.25
-1; 2.3; 4
4; 5 ; 7.8


I would like to generate a plot with the added up weight over x. As you might see, it is difficult to deal with the loads given as extensions, overlapping areas and having no discrete points.

I would be very glad, if you have some ideas for solving this problem or giving some hints! At the moment I'm thinking of evaluating weights at discrete points, adding them up, but I think there is a more correct and satisfying way :-)

-
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  Peter Jansson Mar 12 '13 at 17:33
Could you include a mockup of what you want the result to look like? –  Jake Mar 12 '13 at 17:38
Also can you make your question less specialized? I can't imagine what that distribution is used or looks like. –  percusse Mar 12 '13 at 23:52

You are right, discretization looks to be the simplest way to go. Here is a complete MWE using the asymptote package:

\begin{filecontents*}{./wd.csv}
# x-start [m] | x-stop [m] | weight [t/m]
-3, 5, 2.5
2, 4, 5.6
2.45, 6.33, 0.25
-1, 2.3, 4
4, 5, 7.8
\end{filecontents*}
\documentclass[10pt,a4paper]{article}
\usepackage{asymptote}
\begin{document}
\begin{figure}
\center
\begin{asy}
import graph;
size(200,200,IgnoreAspect);
file fin=input("wd.csv").csv();
real[][] A=fin.dimension(0,3);
A=transpose(A);
real[] xStart=A[0];
real[] xStop=A[1];
real[] weight=A[2];
int rows=xStart.length;
real lmin=min(xStart);
real lmax=max(xStop);
real dl=lmax-lmin; // total length
int n=200; // resolution, the number of intervals
real[] PointY=new real[n+1];
PointY[:]=array(PointY.length,0.0); // initialize with 0
int beg,fin;
for(int i=0;i<rows;++i){
beg=round((xStart[i]-lmin)/dl*n);
fin=round((xStop[i]-lmin)/dl*n);
for(int j=beg;j<=fin;++j){
PointY[j]+=weight[i];
}
}
real[] PointX=new real[PointY.length];
for(int i=0;i<PointX.length;++i){
PointX[i]=i/n*dl+lmin;
}
pen p=orange+2pt;
draw(graph(PointX,PointY),p);
real xmin,xmax;
real ymin,ymax;
xmin=lmin;
xmax=lmax;
ymin=0;
ymax=max(PointY);
xaxis("$x$,\,m",xmin,xmax, RightTicks());
yaxis("$w(x)$,\,t/m",ymin,ymax,
LeftTicks(beginlabel=false,Step=2,step=1)
);
\end{asy}
\end{figure}
\end{document}


Copy this MWE into wd.tex in a separate folder, create file latexmkrc:

sub asy {return system("asy '\$_[0]'");}

and run latexmk -pdf wd.tex. The result looks like