| 318 | } |
| 319 | |
| 320 | bool tooltip::ToolTipController::ShowToolTip(const util::Point &point) { |
| 321 | |
| 322 | util::Point _point = point; |
| 323 | |
| 324 | //限制point.x的坐标为数据点的最后一个坐标 |
| 325 | double maxPointX = FLT_MIN; |
| 326 | double minPointX = FLT_MAX; |
| 327 | auto &geom = chart_->geoms_.front(); |
| 328 | const nlohmann::json &firstRecord = geom->GetFirstSnapRecord(chart_); |
| 329 | const nlohmann::json &lastRecord = geom->GetLastSnapRecord(chart_); |
| 330 | //在各分组中取最大的 当scale为timesharing的时候,first和last可能对调,所以min和max判断都需要 |
| 331 | if (lastRecord.contains("_x")) { |
| 332 | double lastX = json::GetNumber(lastRecord, "_x"); |
| 333 | minPointX = fmin(minPointX, lastX); |
| 334 | maxPointX = fmax(maxPointX, lastX); |
| 335 | } |
| 336 | |
| 337 | if (firstRecord.contains("_x")) { |
| 338 | double firstX = json::GetNumber(firstRecord, "_x"); |
| 339 | minPointX = fmin(minPointX, firstX); |
| 340 | maxPointX = fmax(maxPointX, firstX); |
| 341 | } |
| 342 | _point.x = fmax(fmin(_point.x, maxPointX), minPointX); |
| 343 | |
| 344 | auto timestampStart = xg::CurrentTimestampAtMM(); |
| 345 | nlohmann::json tooltipMarkerItems; |
| 346 | std::for_each(chart_->geoms_.begin(), chart_->geoms_.end(), [&](auto &geom) -> void { |
| 347 | std::string xFieldPrefix = geom->isAdjustDodge() ? "original_" : ""; |
| 348 | nlohmann::json records = geom->GetSnapRecords(chart_, _point, false); |
| 349 | for(std::size_t index = 0; index < records.size(); ++index) { |
| 350 | nlohmann::json &record = records[index]; |
| 351 | if(record.contains("_x") && record.contains("_y")) { |
| 352 | nlohmann::json tooltipItem; |
| 353 | tooltipItem["x"] = record.contains("_original_x") ? record["_original_x"] : record["_x"]; |
| 354 | tooltipItem["y"] = record["_y"]; |
| 355 | tooltipItem["color"] = record.contains("_color") ? record["_color"].get<std::string>() : GLOBAL_COLORS[0]; |
| 356 | tooltipItem["xTip"] = config_["xTip"]; |
| 357 | tooltipItem["yTip"] = config_["yTip"]; |
| 358 | |
| 359 | auto &nameField = config_.contains("yTipField") ? json::GetString(config_, "yTipField") : tooltip::_GetToolTipGroupNameField(chart_, geom); |
| 360 | auto &yScale = chart_->GetScale(nameField); |
| 361 | tooltipItem["name"] = nameField; |
| 362 | |
| 363 | if (json::GetString(config_, "xPositionType") == "record") { |
| 364 | _point.x = record.contains("_original_x") ? record["_original_x"] : record["_x"]; |
| 365 | } |
| 366 | if (json::GetString(config_, "yPositionType") == "record") { |
| 367 | _point.y = record["_y"]; |
| 368 | } |
| 369 | tooltipItem["value"] = InvertYTip(_point, yScale); |
| 370 | auto &xField = geom->GetXScaleField(); |
| 371 | if (xFieldPrefix.length() > 0) { |
| 372 | tooltipItem["title"] = chart_->GetScale(xField).GetTickText(record[xFieldPrefix + xField], chart_, index); |
| 373 | } else { |
| 374 | tooltipItem["title"] = chart_->GetScale(xField).GetTickText(record[xField], chart_, index); |
| 375 | } |
| 376 | |
| 377 | tooltipItem["touchX"] = _point.x; |
no test coverage detected