Guest User

Untitled

a guest
Oct 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. \ProvidesPackage{google_style_donut_chart}[2017/10/220 v1.0 Donut chart]
  2.  
  3. \usepackage{tikz}
  4. \usepackage{xcolor}
  5.  
  6. \definecolor{chartColor1}{HTML}{4285F4}
  7. \definecolor{chartColor2}{HTML}{DB4437}
  8. \definecolor{chartColor3}{HTML}{F4B400}
  9. \definecolor{chartColor4}{HTML}{0F9D58}
  10. \definecolor{chartColor5}{HTML}{AB47BC}
  11. \definecolor{chartColor6}{HTML}{00ACC1}
  12.  
  13. % Adjusts the size of the wheel:
  14. \def\innerradius{2.0cm}
  15. \def\outerradius{4.2cm}
  16.  
  17. % I found somewhere on the internet an template for this and I extended to fit.
  18. %
  19. % The default usage:
  20. % \wheelchart{28.3/chartColor1/Loading, 775.5/chartColor2/Scripting, 79.3/chartColor3/Rendering, 16/charColor4/Painting, 163/chartColor5/Other}
  21. \newcommand{\wheelchart}[1]{
  22. % Calculate total
  23. \pgfmathsetmacro{\totalnum}{0}
  24. \foreach \value/\color/\name [count=\xi] in {#1} {
  25. \pgfmathparse{\value+\totalnum}
  26. \global\let\totalnum=\pgfmathresult
  27. \global\let\allnum=\xi
  28. }
  29.  
  30. \begin{tikzpicture}
  31. % Calculate the thickness and the middle line of the wheel
  32. \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
  33. \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}
  34.  
  35. % Rotate so we start from the top
  36. \begin{scope}[line width=\wheelwidth,rotate=90]
  37.  
  38. % Loop through each value set. \cumnum keeps track of where we are in the wheel
  39. \pgfmathsetmacro{\cumnum}{0}
  40. \foreach \value/\color/\name [count=\xi] in {#1} {
  41. \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
  42.  
  43. % Calculate the percent value
  44. \pgfmathsetmacro{\percentage}{\value/\totalnum*100}
  45. % Calculate the mid angle of the color segments to place the labels
  46. \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
  47.  
  48. % This is necessary for the labels to align nicely
  49. \pgfmathparse{
  50. (-\midangle<5?"south":
  51. (-\midangle<85?"south west":
  52. (-\midangle<105?"west":
  53. (-\midangle<175?"north west":
  54. (-\midangle<185?"north":
  55. (-\midangle<265?"north east":
  56. (-\midangle<275?"east":
  57. (-\midangle<355?"south east":"south")
  58. )
  59. )
  60. )
  61. )
  62. )
  63. )
  64. )
  65. } \edef\textanchor{\pgfmathresult}
  66.  
  67. % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
  68. \draw[color=\color] (-\cumnum:\midradius pt) arc (-\cumnum:-(\newcumnum):\midradius pt);
  69.  
  70. % Draw the data labels
  71. \node [color=\color] at (\allnum/2-\xi, 7.0){\name: \value};
  72.  
  73. % Set the old cumulated angle to the new value
  74. \global\let\cumnum=\newcumnum
  75. }
  76.  
  77. \end{scope}
  78. \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
  79. \end{tikzpicture}
  80. }
Add Comment
Please, Sign In to add comment