more improvements

This commit is contained in:
Sergei Trifonov 2022-06-17 18:02:03 +02:00
parent 1d9bcb5993
commit 85dd3afa2b
3 changed files with 79 additions and 40 deletions

View File

@ -8,7 +8,6 @@
} }
rect.zoom-panel { rect.zoom-panel {
/*cursor: ew-resize;*/
fill: none; fill: none;
pointer-events: all; pointer-events: all;
} }
@ -20,18 +19,18 @@ rect.zoom-panel {
} }
.axis.y { .axis.y {
font-size: 16px; font-size: 9px;
cursor: ns-resize; cursor: ns-resize;
} }
.axis.x { .axis.x {
font-size: 16px; font-size: 9px;
} }
#ruler { #ruler {
text-anchor: middle; text-anchor: middle;
alignment-baseline: before-edge; alignment-baseline: before-edge;
font-size: 16px; font-size: 9px;
font-family: sans-serif; font-family: sans-serif;
pointer-events: none; pointer-events: none;
} }

View File

@ -14,12 +14,22 @@
<script language="javascript" type="text/javascript" src="js/d3.v4.min.js"></script> <script language="javascript" type="text/javascript" src="js/d3.v4.min.js"></script>
<script language="javascript" type="text/javascript" src="js/d3-tip-0.8.0-alpha.1.js"></script> <script language="javascript" type="text/javascript" src="js/d3-tip-0.8.0-alpha.1.js"></script>
<script language="javascript" type="text/javascript" src="js/d3-gantt.js"></script> <script language="javascript" type="text/javascript" src="js/d3-gantt.js"></script>
<div class="container-fluid" id="toolbar" style="margin:20px;margin-right:50px">
<div class="pull-right"> <nav class="navbar navbar-default">
<button type="button" class="btn btn-primary" id="toolbar-load" data-toggle="modal" data-target="#loadModal">Load</button> <div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">ClickHouse trace-visualizer</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form class="navbar-form navbar-left">
<button type="button" class="btn btn-primary" id="toolbar-load" data-toggle="modal" data-target="#loadModal">Load</button>
</form>
</div>
</div> </div>
</div> </nav>
<div id="placeholder" class="chart-placeholder"></div> <div id="placeholder" class="chart-placeholder"></div>
<div class="modal fade" id="loadModal" tabindex="-1" role="dialog" aria-labelledby="loadModalLabel"> <div class="modal fade" id="loadModal" tabindex="-1" role="dialog" aria-labelledby="loadModalLabel">
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
<div class="modal-content"> <div class="modal-content">
@ -42,21 +52,36 @@
</html> </html>
<script language="javascript"> <script language="javascript">
var example_json = [ var example_json = [];
{ t1: 100, t2: 200, band: "band1", color: "#888", text: "text1" }, var example_colors = ["#f88", "#8f8", "#88f", "#888", "#aaa"];
{ t1: 300, t2: 400, band: "band2", color: "#ff8", text: "text2" }, for (let i = 0; i < 1024; i++) {
{ t1: 100, t2: 400, band: "band3", color: "#888", text: "some very long text with a lot of letters in it" }, example_json.push({
{ t1: 300, t2: 400, band: "band1", color: "#8ff", text: "some_very_long_identifier_with_a_lot_of_letters_in_it" }, t1: i + 50 - Math.random() * 60,
{ t1: 500, t2: 800, band: "band2", color: "#f8f", text: "test\nif\nnew\nline\nworks\nhere?" } t2: i + 50 + Math.random() * 60,
]; band: "band" + (i % 128),
color: example_colors[i % example_colors.length],
text: "it is span #" + i
});
}
example_json.bands = new Set(example_json.map(x => x.band));
example_json.max_time_ms = Math.max(...example_json.map(x => x.t2));
let chart_height = window.innerHeight - $("#placeholder")[0].getBoundingClientRect().y - 80;
var data = null; var data = null;
var chart = null; var chart = null;
function renderChart(parsed) { function renderChart(parsed) {
data = parsed; data = parsed;
chart = d3.gantt().height(chart_height).selector("#placeholder"); if (chart != null) {
chart.destroy();
}
let view_height = window.innerHeight - $("#placeholder")[0].getBoundingClientRect().y - 40;
let data_height = parsed.bands.size * 8;
chart = d3.gantt()
.height(Math.max(view_height, data_height))
.view_height(view_height)
.selector("#placeholder")
.timeDomain([0, parsed.max_time_ms])
;
chart(data); chart(data);
} }
@ -139,18 +164,22 @@
} }
let result = []; let result = [];
let bands = new Set();
for (let i = 0; i < json.data.length; i++) { for (let i = 0; i < json.data.length; i++) {
let span = json.data[i]; let span = json.data[i];
let band = Object.values(span.group).join(' ');
bands.add(band);
result.push({ result.push({
t1: convertTime(+span.start_time_us), t1: convertTime(+span.start_time_us),
t2: convertTime(+span.finish_time_us), t2: convertTime(+span.finish_time_us),
band: Object.values(span.group).join(' '), band,
color: d3.interpolateRainbow((strHash(span.color) % 256) / 256), color: d3.interpolateRainbow((strHash(span.color) % 256) / 256),
text: span.operation_name text: span.operation_name
}); });
} }
chart.timeDomain([0, max_time_ms]); result.bands = bands;
result.max_time_ms = max_time_ms;
return result; return result;
} }

View File

@ -8,9 +8,6 @@
initAxis(); initAxis();
// clear previous chart (if any)
d3.select(selector).selectAll("svg").remove();
// create svg element // create svg element
svg = d3.select(selector) svg = d3.select(selector)
.append("svg") .append("svg")
@ -40,6 +37,7 @@
.on("zoom", function() { .on("zoom", function() {
if (tipShown != null) { if (tipShown != null) {
tip.hide(tipShown); tip.hide(tipShown);
tipShown = null;
} }
var tr = d3.event.transform; var tr = d3.event.transform;
xZoomed = tr.rescaleX(x); xZoomed = tr.rescaleX(x);
@ -54,7 +52,7 @@
zoomContainer1.attr("transform", "translate(" + tr.x + ",0) scale(" + tr.k + ",1)"); zoomContainer1.attr("transform", "translate(" + tr.x + ",0) scale(" + tr.k + ",1)");
zoomContainer2.attr("transform", "translate(" + tr.x + ",0) scale(" + tr.k + ",1)"); zoomContainer2.attr("transform", "translate(" + tr.x + ",0) scale(" + tr.k + ",1)");
render(); //render();
}) })
.on("start", function() { .on("start", function() {
zoom.startScreenY = d3.event.sourceEvent.screenY; zoom.startScreenY = d3.event.sourceEvent.screenY;
@ -119,6 +117,12 @@
.call(xAxis) .call(xAxis)
; ;
// ruler should be drawn above x axis and under y axis
ruler = fixedContainer.append("g")
.attr("id", "ruler")
.attr("transform", "translate(0, 0)")
;
// create y axis // create y axis
fixedContainer.append("g") fixedContainer.append("g")
.attr("class", "y axis") .attr("class", "y axis")
@ -189,10 +193,6 @@
; ;
// ruler // ruler
ruler = fixedContainer.append("g")
.attr("id", "ruler")
.attr("transform", "translate(0, 0)")
;
ruler.append("rect") ruler.append("rect")
.attr("id", "ruler-line") .attr("id", "ruler-line")
.attr("x", 0) .attr("x", 0)
@ -221,10 +221,9 @@
// scroll handling // scroll handling
window.onscroll = function myFunction() { window.onscroll = function myFunction() {
documentBodyScrollLeft(document.body.scrollLeft); documentBodyScrollLeft(window.scrollX);
documentBodyScrollTop(document.body.scrollTop); documentBodyScrollTop(window.scrollY);
var scroll = scrollParams(); var scroll = scrollParams();
svgChartContainer svgChartContainer
.attr("transform", "translate(" + margin.left .attr("transform", "translate(" + margin.left
+ ", " + (margin.top + scroll.y1) + ")"); + ", " + (margin.top + scroll.y1) + ")");
@ -285,7 +284,7 @@
.attr("class", "bar") .attr("class", "bar")
.attr("vector-effect", "non-scaling-stroke") .attr("vector-effect", "non-scaling-stroke")
.style("fill", d => d.color) .style("fill", d => d.color)
.on('click', function(d) { .on('mouseover', function(d) {
if (tipShown != d) { if (tipShown != d) {
tipShown = d; tipShown = d;
tip.show(d); tip.show(d);
@ -317,12 +316,11 @@
//.clamp(true); // dosn't work with zoom/pan //.clamp(true); // dosn't work with zoom/pan
xZoomed = x; xZoomed = x;
y = d3.scaleBand() y = d3.scaleBand()
.domain(Object.values(data).map(d => d.band).sort()) .domain([...data.bands])
.rangeRound([0, height - margin.top - margin.bottom]) .range([1, height - margin.top - margin.bottom])
.padding(0.5); .padding(1/8);
xAxis = d3.axisBottom() xAxis = d3.axisBottom()
.scale(x) .scale(x)
//.tickSubdivide(true)
.tickSize(8) .tickSize(8)
.tickPadding(8); .tickPadding(8);
yAxis = d3.axisLeft() yAxis = d3.axisLeft()
@ -334,7 +332,7 @@
var documentBodyScrollLeft = function(value) { var documentBodyScrollLeft = function(value) {
if (!arguments.length) { if (!arguments.length) {
if (documentBodyScrollLeft.value === undefined) { if (documentBodyScrollLeft.value === undefined) {
documentBodyScrollLeft.value = document.body.scrollLeft; documentBodyScrollLeft.value = window.scrollX;
} }
return documentBodyScrollLeft.value; return documentBodyScrollLeft.value;
} else { } else {
@ -346,7 +344,7 @@
var documentBodyScrollTop = function(value) { var documentBodyScrollTop = function(value) {
if (!arguments.length) { if (!arguments.length) {
if (!documentBodyScrollTop.value === undefined) { if (!documentBodyScrollTop.value === undefined) {
documentBodyScrollTop.value = document.body.scrollTop; documentBodyScrollTop.value = window.scrollY;
} }
return documentBodyScrollTop.value; return documentBodyScrollTop.value;
} else { } else {
@ -356,7 +354,7 @@
var scrollParams = function() { var scrollParams = function() {
var y1 = documentBodyScrollTop(); var y1 = documentBodyScrollTop();
var y2 = y1 + window.innerHeight - margin.footer; var y2 = y1 + view_height;
y2 = Math.min(y2, height - margin.top - margin.bottom); y2 = Math.min(y2, height - margin.top - margin.bottom);
var h = y2 - y1; var h = y2 - y1;
return { return {
@ -404,7 +402,7 @@
var textWidth = 10 * posText.length; var textWidth = 10 * posText.length;
ruler.select("#bgrect") ruler.select("#bgrect")
.attr("x", -textWidth/2 - xpadding) .attr("x", -textWidth/2 - xpadding)
.attr("y", positionRuler.bbox.y - ypadding) .attr("y", positionRuler.bbox.y - ypadding + window.scrollY)
.attr("width", textWidth + (xpadding*2)) .attr("width", textWidth + (xpadding*2))
.attr("height", positionRuler.bbox.height + (ypadding*2)) .attr("height", positionRuler.bbox.height + (ypadding*2))
; ;
@ -428,6 +426,13 @@
return gantt; return gantt;
} }
gantt.view_height = function(value) {
if (!arguments.length)
return view_height;
view_height = +value;
return gantt;
}
gantt.selector = function(value) { gantt.selector = function(value) {
if (!arguments.length) if (!arguments.length)
return selector; return selector;
@ -447,12 +452,18 @@
return data; return data;
} }
gantt.destroy = function() {
tip.destroy();
d3.select(selector).selectAll("svg").remove();
}
// constructor // constructor
// Config // Config
var margin = { top: 20, right: 40, bottom: 20, left: 200, footer: 100 }, var margin = { top: 0, right: 30, bottom: 20, left: 150 },
height = document.body.clientHeight - margin.top - margin.bottom - 5, height = document.body.clientHeight - margin.top - margin.bottom - 5,
width = document.body.clientWidth - margin.right - margin.left - 5, width = document.body.clientWidth - margin.right - margin.left - 5,
view_height = window.innerHeight,
selector = 'body', selector = 'body',
timeDomainStart = 0, timeDomainStart = 0,
timeDomainEnd = 1000, timeDomainEnd = 1000,