| 7 | using namespace xg; |
| 8 | |
| 9 | util::Point guide::GuideBase::GetPosition(XChart &chart, const nlohmann::json &position, const std::string &xField, const std::string &yField) { |
| 10 | if (!position.is_array() || position.size() != 2) { |
| 11 | return util::Point(0, 0); |
| 12 | } |
| 13 | |
| 14 | scale::AbstractScale &xScale = chart.GetScale(xField); |
| 15 | scale::AbstractScale &yScale = chart.GetScale(yField); |
| 16 | |
| 17 | int geometryIndex = 0; |
| 18 | if (this->config_.contains("geometryIndex")) { |
| 19 | geometryIndex = json::GetIntNumber(this->config_, "geometryIndex", 0); |
| 20 | if (geometryIndex < 0 || geometryIndex >= chart.GetGeoms().size()) { |
| 21 | geometryIndex = 0; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | const nlohmann::json &records = chart.GetSelectedRecordsForGeom(geometryIndex); |
| 26 | |
| 27 | bool hasSelectedRecord = false; |
| 28 | if (records.size() > 0) { |
| 29 | hasSelectedRecord = true; |
| 30 | chart.GetLogTracer()->trace("#GetPosition xField:%s yField:%s record:%s", xField.data(), yField.data(), records[0].dump().c_str()); |
| 31 | } |
| 32 | |
| 33 | double x = 0; |
| 34 | if (position[0].is_string()) { |
| 35 | std::string xFieldCfg = position[0]; |
| 36 | if (xFieldCfg == "min") { |
| 37 | x = 0; |
| 38 | } else if (xFieldCfg == "median") { |
| 39 | x = 0.5; |
| 40 | } else if (xFieldCfg == "max") { |
| 41 | x = 1; |
| 42 | } else if (xFieldCfg == "selected" && hasSelectedRecord) { |
| 43 | x = xScale.Scale(records[0][xField]); |
| 44 | } else { |
| 45 | x = xScale.Scale(position[0]); |
| 46 | } |
| 47 | } else { |
| 48 | x = xScale.Scale(position[0]); |
| 49 | } |
| 50 | |
| 51 | double y = 0; |
| 52 | if (position[1].is_string()) { |
| 53 | std::string yFieldCfg = position[1]; |
| 54 | if (yFieldCfg == "min") { |
| 55 | y = 0; |
| 56 | } else if (yFieldCfg == "median") { |
| 57 | y = 0.5; |
| 58 | } else if (yFieldCfg == "max") { |
| 59 | y = 1; |
| 60 | } else if (yFieldCfg == "selected" && hasSelectedRecord) { |
| 61 | y = yScale.Scale(records[0][yField]); |
| 62 | } else { |
| 63 | y = yScale.Scale(position[1]); |
| 64 | } |
| 65 | } else { |
| 66 | y = yScale.Scale(position[1]); |
no test coverage detected