Spaces:
Configuration error
Configuration error
File size: 9,223 Bytes
369e574 14b70ba 369e574 14b70ba 369e574 14b70ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# components/visualization.py
class D3Visualizer:
"""D3.js visualization component"""
@staticmethod
def create_interactive_plot(plot_type: str, data: dict) -> str:
"""Create interactive D3 visualization"""
# Base CSS for visualizations
base_css = """
<style>
.visualization-container { width: 100%; height: 500px; }
.bar { fill: steelblue; }
.bar:hover { fill: brown; }
.line { fill: none; stroke: steelblue; stroke-width: 2; }
.area { fill: steelblue; opacity: 0.2; }
.tooltip { position: absolute; padding: 8px; background: white; border: 1px solid #ddd; border-radius: 4px; }
.axis-label { font-size: 12px; }
</style>
"""
if plot_type == "distribution":
return base_css + f"""
<div id="distribution-plot" class="visualization-container"></div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
(function() {{
const values = {data['values']};
const margin = {{top: 40, right: 40, bottom: 60, left: 60}};
const width = 800 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
// Create SVG
const svg = d3.select("#distribution-plot")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${{margin.left}},${{margin.top}})`);
// Create scales
const x = d3.scaleLinear()
.domain([d3.min(values), d3.max(values)])
.range([0, width]);
const histogram = d3.histogram()
.domain(x.domain())
.thresholds(x.ticks(20));
const bins = histogram(values);
const y = d3.scaleLinear()
.domain([0, d3.max(bins, d => d.length)])
.range([height, 0]);
// Create bars
svg.selectAll(".bar")
.data(bins)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", d => x(d.x0))
.attr("y", d => y(d.length))
.attr("width", d => Math.max(0, x(d.x1) - x(d.x0) - 1))
.attr("height", d => height - y(d.length))
.on("mouseover", function(event, d) {{
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(
`Range: ${{d.x0.toFixed(2)}} - ${{d.x1.toFixed(2)}}<br/>` +
`Count: ${{d.length}}`
)
.style("left", (event.pageX + 5) + "px")
.style("top", (event.pageY - 28) + "px");
}})
.on("mouseout", function(d) {{
tooltip.transition()
.duration(500)
.style("opacity", 0);
}});
// Add axes
svg.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(0,${{height}})`)
.call(d3.axisBottom(x))
.append("text")
.attr("class", "axis-label")
.attr("x", width/2)
.attr("y", 40)
.text("Value");
svg.append("g")
.attr("class", "y-axis")
.call(d3.axisLeft(y))
.append("text")
.attr("class", "axis-label")
.attr("transform", "rotate(-90)")
.attr("y", -40)
.attr("x", -height/2)
.style("text-anchor", "middle")
.text("Frequency");
// Add tooltip
const tooltip = d3.select("#distribution-plot")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
}})();
</script>
"""
elif plot_type == "forecast":
return base_css + f"""
<div id="forecast-plot" class="visualization-container"></div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
(function() {{
const data = {data};
const margin = {{top: 40, right: 40, bottom: 60, left: 60}};
const width = 800 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
// Create SVG
const svg = d3.select("#forecast-plot")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${{margin.left}},${{margin.top}})`);
// Create scales
const x = d3.scaleLinear()
.domain([0, data.time.length-1])
.range([0, width]);
const y = d3.scaleLinear()
.domain([
d3.min(data.lower),
d3.max(data.upper)
])
.range([height, 0]);
// Create area
const area = d3.area()
.x((d, i) => x(i))
.y0(d => y(d[0]))
.y1(d => y(d[1]));
// Add confidence interval area
svg.append("path")
.datum(data.time.map((t, i) => [data.lower[i], data.upper[i]]))
.attr("class", "area")
.attr("d", area);
// Add mean line
const line = d3.line()
.x((d, i) => x(i))
.y(d => y(d));
svg.append("path")
.datum(data.mean)
.attr("class", "line")
.attr("d", line);
// Add axes
svg.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(0,${{height}})`)
.call(d3.axisBottom(x))
.append("text")
.attr("class", "axis-label")
.attr("x", width/2)
.attr("y", 40)
.text("Time Period");
svg.append("g")
.attr("class", "y-axis")
.call(d3.axisLeft(y))
.append("text")
.attr("class", "axis-label")
.attr("transform", "rotate(-90)")
.attr("y", -40)
.attr("x", -height/2)
.style("text-anchor", "middle")
.text("Value");
// Add tooltip for hover
const tooltip = d3.select("#forecast-plot")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Add hover interaction
const bisect = d3.bisector(d => d).left;
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.style("fill", "none")
.style("pointer-events", "all")
.on("mousemove", function(event) {{
const x0 = x.invert(d3.pointer(event)[0]);
const i = Math.round(x0);
if (i >= 0 && i < data.time.length) {{
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(
`Time: ${{data.time[i]}}<br/>` +
`Mean: ${{data.mean[i].toFixed(2)}}<br/>` +
`Range: [${{data.lower[i].toFixed(2)}}, ${{data.upper[i].toFixed(2)}}]`
)
.style("left", (event.pageX + 5) + "px")
.style("top", (event.pageY - 28) + "px");
}}
}})
.on("mouseout", function() {{
tooltip.transition()
.duration(500)
.style("opacity", 0);
}});
}})();
</script>
"""
return "Unsupported visualization type" |