chart.d 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. module renderable.chart;
  2. import renderable.renderable;
  3. import color;
  4. import vector;
  5. import cell;
  6. import renderable.renderable;
  7. import horizontaltextalignment;
  8. import verticaltextalignment;
  9. import renderable.rect;
  10. import std.algorithm;
  11. import renderable.stacklayout;
  12. import stacklayouttype;
  13. import renderable.cellcachecontainer;
  14. import charttype;
  15. class Chart : Renderable {
  16. ChartType chartType;
  17. int[] data;
  18. int columnWidth;
  19. int columnSpace;
  20. Color chartColor;
  21. Color backgroundColor;
  22. private CellCacheContainer container;
  23. this(ChartType chartType, int[] data, int columnWidth, int columnSpace, Color chartColor = Color.White, Color backgroundColor = Color.terminal()) {
  24. Vector dimension = Vector(data.maxElement, cast(int)((data.length * columnSpace) + (data.length * columnWidth) - columnSpace));
  25. if (chartType == ChartType.bar) {
  26. this.dimension = dimension;
  27. } else {
  28. this.dimension = Vector(dimension. y , dimension. x );
  29. }
  30. this.chartType = chartType;
  31. this.data = data;
  32. this.columnWidth = columnWidth;
  33. this.columnSpace = columnSpace;
  34. this.chartColor = chartColor;
  35. this.backgroundColor = backgroundColor;
  36. container = new CellCacheContainer();
  37. }
  38. override Cell[] render() {
  39. foreach (indx, num; data) {
  40. Rect rect = Rect.withFill(Vector(columnWidth, num), chartColor);
  41. if (chartType == ChartType.bar) {
  42. rect.dimension = Vector(rect.dimension. y , rect.dimension. x );
  43. }
  44. Vector coordinates = Vector(0, cast(int)((indx * columnSpace) + (indx * columnWidth)));
  45. if (chartType == ChartType.bar) {
  46. container.updateCache(rect, coordinates);
  47. } else {
  48. container.updateCache(rect, Vector(coordinates.y, this.dimension. y - num));
  49. }
  50. }
  51. if (backgroundColor != Color.terminal()) {
  52. container.updateCache(Rect.withFill(Vector(dimension. x , dimension. y ), backgroundColor), Vector(0, 0));
  53. }
  54. return container.cache;
  55. }
  56. }